torvald_helmer Posted April 15, 2007 Share Posted April 15, 2007 I have two arrays, $arr1 and $arr2. Values in the first corresponds to the same index-values in the second. How do I sort these array's ascending according to $arr1? Link to comment https://forums.phpfreaks.com/topic/47115-how-to-sort-multiple-arrays/ Share on other sites More sharing options...
Barand Posted April 15, 2007 Share Posted April 15, 2007 array_multisort <?php $a = array(2,3,1); $b = array('a', 'b', 'c'); array_multisort($a, $b); echo '<pre>', print_r($a, true), '</pre>'; echo '<pre>', print_r($b, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/47115-how-to-sort-multiple-arrays/#findComment-229762 Share on other sites More sharing options...
Voldemort Posted April 15, 2007 Share Posted April 15, 2007 Or if both of the arrays hold single corresponding values, you might want to try using an associative array, and then ksort() it. <?php $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); ksort($fruits); foreach ($fruits as $key => $val) { echo "$key = $val\n"; } ?> changes the array to be sort by the key. If you want it sorted by the value, use asort($fruits) instead. Output is a = orange b = banana c = apple d = lemon That's taken from the php.net page on ksort, http://www.php.net/manual/en/function.ksort.php Link to comment https://forums.phpfreaks.com/topic/47115-how-to-sort-multiple-arrays/#findComment-229764 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.