You want to sort multiple arrays or an array with multiple dimensions.
Use array_multisort(). To sort mutiple arrays simultaneously, pass multiple arrays. To sort multiple dimensions within a single array, pass the specific array elements.
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.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $colors = array('red', 'yellow', 'green'); $fruits_original = array('yellow'=>'Bananas', 'red'=>'Apples', 'green'=>'Grapes'); echo "<p><b>This is colors array.</b></p>"; print_r($colors); echo "<p><b>This is fruits_original array.</b></p>"; print_r($fruits_original); echo "<p><b>Use array_multisort() to sort multiple arrays simultaneously.</b></p>"; array_multisort($colors, $fruits_original); echo "<p><b>This is colors after sorting.</b></p>"; print_r($colors); echo "<p><b>This is fruits_original after sorting.</b></p>"; print_r($fruits_original); echo "<p><b>This is a multidimensional array.</b></p>"; $fruits = array('red' => array('Watermelons', 'Apples', 'Strawberries'), 'yellow' => array('Mangoes', 'Bananas', 'Pineapples'), 'green' => array('Grapes', 'Pears', 'Kiwis') ); foreach ($fruits as $color=>$color_fruits) { // $color_fruits is an array foreach($color_fruits as $key=>$value) { echo "\$fruit[$color][$key] = $value. <br />"; } } echo "<p><b>This is to sort multiple dimensions within a single array.</b></p>"; array_multisort($fruits['red'], $fruits['yellow'], $fruits['green']); foreach ($fruits as $color=>$color_fruits) { // $color_fruits is an array foreach($color_fruits as $key=>$value) { echo "\$fruit[$color][$key] = $value. <br />"; } } ?> </p> </body> </html> |
To modify the sort type, as in sort(), pass in SORT_REGULAR (default), SORT_NUMERIC, or SORT_STRING after the array.
To modify the sort order, unlike in sort(), pass in SORT_ASC (default) or SORT_DESC after the array. You can also pass in both a sort type and a sort order after the array.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $colors = array('red', 'yellow', 'green'); $fruits_original = array('yellow'=>'Bananas', 'red'=>'Apples', 'green'=>'Grapes'); echo "<p><b>This is colors array.</b></p>"; print_r($colors); echo "<p><b>This is fruits_original array.</b></p>"; print_r($fruits_original); echo "<p><b>Use array_multisort() to sort multiple arrays simultaneously.</b></p>"; array_multisort($colors, SORT_STRING, SORT_DESC, $fruits_original, SORT_STRING, SORT_DESC); echo "<p><b>This is colors after sorting SORT_STRING, SORT_DESC.</b></p>"; print_r($colors); echo "<p><b>This is fruits_original after sorting SORT_STRING, SORT_DESC.</b></p>"; print_r($fruits_original); echo "<p><b>This is a multidimensional array.</b></p>"; $fruits = array('red' => array('Watermelons', 'Apples', 'Strawberries'), 'yellow' => array('Mangoes', 'Bananas', 'Pineapples'), 'green' => array('Grapes', 'Pears', 'Kiwis') ); foreach ($fruits as $color=>$color_fruits) { // $color_fruits is an array foreach($color_fruits as $key=>$value) { echo "\$fruit[$color][$key] = $value. <br />"; } } echo "<p><b>This is to sort multiple dimensions within a single array.</b></p>"; array_multisort($fruits['red'], SORT_STRING, SORT_DESC, $fruits['yellow'], SORT_STRING, SORT_DESC, $fruits['green'], SORT_STRING, SORT_DESC); foreach ($fruits as $color=>$color_fruits) { // $color_fruits is an array foreach($color_fruits as $key=>$value) { echo "\$fruit[$color][$key] = $value. <br />"; } } $numbers = array(0, 1, 2, 3); $letters = array('a', 'b', 'c', 'd'); echo "<p><b>This is numbers array.</b></p>"; print_r($numbers); echo "<p><b>This is letters array.</b></p>"; print_r($letters); array_multisort($numbers, SORT_NUMERIC, SORT_DESC, $letters, SORT_STRING, SORT_DESC); echo "<p><b>After sorting SORT_NUMERIC, SORT_DESC this is numbers array.</b></p>"; print_r($numbers); echo "<p><b>After sorting SORT_STRING, SORT_DESC this is letters array.</b></p>"; print_r($letters); ?> </p> </body> </html> |