Sorting Multiple Arrays based on one array - array_multisort
by rajesh[ Edit ] 2010-01-02 15:54:08
We can sort multiple arrays based on the values of the first array at once using array_multisort function.
Example:
$a = array(c,a,d,b);
$b = array(5,3,2,7);
to sort the arrays at one use
array_multisort($a, $b);
If you want array be to be sorted in ascending / descending order just use the optional args SORT_DESC, SORT_ASC.
We also have args SORT_NUMERIC, SORT_STRING to define as string and number.
Example:
<?php
$a = array(c,a,d,b);
$b = array(5,3,2,7);
array_multisort($a, SORT_STRING, SORT_ASC, $b);
print_r($a);
print_r($b);
?>
Result:Array a = a, b, c, d
Array b = 3, 7, 5, 2
Now you can see that array a is sorted and values in corresponding positions of array b has also been moved.