4.19. Sorting Multiple Arrays
$colors = array('Red', 'White', 'Blue'); $cities = array('Boston', 'New York', 'Chicago'); array_multisort($colors, $cities); print_r($colors); print_r($cities); Array (  => Blue  => Red  => White ) Array (  => Chicago  => Boston  => New York )
$stuff = array('colors' => array('Red', 'White', 'Blue'), 'cities' => array('Boston', 'New York', 'Chicago')); array_multisort($stuff['colors'], $stuff['cities']); print_r($stuff); Array ( [colors] => Array (  => Blue  => Red  => White ) [cities] => Array (  => Chicago  => Boston  => New York ) )
To modify the sort type, as in sort( ), pass in SORT_REGULAR, SORT_NUMERIC, or SORT_STRING after the array. To modify the sort order, unlike in sort( ), pass in SORT_ASC or SORT_DESC after the array. You can also pass in both a sort type and a sort order after the array.
The array_multisort( ) function can sort several arrays at once or a multidimensional array by one or more dimensions. The arrays are treated as columns of a table to be sorted by rows. The first array is the main one to sort by; all the items in the other arrays are reordered based on the sorted order of the first array. If items in the first array compare as equal, the sort order is determined by the second array, and so on.
The default sorting values are SORT_REGULAR and SORT_ASC, and they're reset after each array, so there's no reason to pass either of these two values, except for clarity.
$numbers = array(0, 1, 2, 3); $letters = array('a', 'b', 'c', 'd'); array_multisort($numbers, SORT_NUMERIC, SORT_DESC, $letters, SORT_STRING , SORT_DESC);
This example reverses the arrays.
4.19.4. See Also
Copyright © 2003 O'Reilly & Associates. All rights reserved.