There are several ways to sort arrays in PHP, and this article is to share the implementation of multi-dimensional array sorting using PHP's Array_multisort () function, and the ordering of multidimensional arrays by custom functions.
Sort using PHP's own Array_multisort function
<?php $data = Array (); $data [] = Array (' volume ' = +, ' edition ' = 2); $data [] = Array (' volume ' = +, ' edition ' = 1); $data [] = Array (' volume ' = =, ' edition ' = 6); $data [] = Array (' volume ' = = 98, ' edition ' = 2); $data [] = Array (' volume ' = +, ' edition ' = 6); $data [] = Array (' volume ' = +, ' edition ' = 7); Get the list of columns foreach ($data as $key + = $row) { $volume [$key] = $row [' volume ']; $edition [$key] = $row [' edition ']; } Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data); Print_r ($data);? >
Output Result:
Array ( [0] = = Array ( [Volume] = 98 [Edition] = 2 ) [1] = = Array ( [ Volume] [ Edition] = 1 ) [2] = = Array ( [Volume] = [ Edition] = 6< c14/>) [3] = = Array ( [Volume] = [Edition] = 6 ) [4] = = Array ( [Volume] = [Edition] + 2 ) [5] = = Array ([volume] = [ Edition] = 7 ))
There are also more detailed instructions on Array_multisort official documents.
II. Ordering of custom functions 1
<?php $data = Array (); $data [] = Array (' volume ' = +, ' edition ' = 2); $data [] = Array (' volume ' = +, ' edition ' = 1); $data [] = Array (' volume ' = =, ' edition ' = 6); $data [] = Array (' volume ' = = 98, ' edition ' = 2); $data [] = Array (' volume ' = +, ' edition ' = 6); $data [] = Array (' volume ' = +, ' edition ' = 7); Get the list of columns foreach ($data as $key + = $row) {$volume [$key] = $row [' volume ']; $edition [$key] = $row [' Edition ']; } $ret = ArraySort ($data, ' volume ', ' desc '); Print_r ($ret); /** * @desc arraysort php Two-dimensional array sort sorted by the specified key * @param array $arr will be sorted * @param string $keys Specify the sorted key * @param string $type sort type ASC | DESC * @return Array */function ArraySort ($arr, $keys, $type = ' asc ') {$keysvalue = $new _array = array (); foreach ($arr as $k = + $v) {$keysvalue [$k] = $v [$keys]; } $type = = ' ASC '? Asort ($keysvalue): ARsort ($keysvalue); Reset ($keysvalue); foreach ($keysvalue as $k = + $v) {$new _array[$k] = $arr [$k]; } return $new _array; }?>
Output Result:
Array ( [3] = = Array ( [Volume] = 98 [Edition] = 2 ) [4] = = Array ( [ Volume] [ Edition] = 6 ) [1] = = Array ( [Volume] = [ Edition] = 1< c14/>) [2] = = Array ( [Volume] = [Edition] = 6 ) [5] = = Array ( [Volume] = [Edition] + 7 ) [0] = = Array ([volume] = [ Edition] = 2 ))
One of the differences between this custom function and the system function is that the custom function only supports sorting on a key, and it needs to be executed multiple times to support the ordering of multiple keys; While the system function Array_multisort can be one-time to multiple keys and can specify more than one collation, the system function is quite powerful, recommend the use of system functions, after all, is the C bottom-level implementation, here is just an example of how to sort the array by a custom function, Of course, this custom function can also continue to expand to support more collations. In the rankings, leaderboards, scores and other scenes used in a lot of.
III. Ordering of custom functions 2
The following function is to sort a given two-dimensional array according to the specified key value, and first look at the function definition:
function Array_sort ($arr, $keys, $type = ' asc ') { $keysvalue = $new _array = Array (); foreach ($arr as $k + = $v) { $ keysvalue[$k] = $v [$keys]; } if ($type = = ' asc ') { asort ($keysvalue);} else{ Arsort ($keysvalue), Reset ($keysvalue), foreach ($keysvalue as $k = = $v) { $new _array[$k] = $arr [$k];} return $new _array; }
It can sort the two-dimensional arrays by the specified key values, or you can specify ascending or descending sorting (the default is ascending), and the usage example:
$array = Array (' name ' = ' phone ', ' brand ' = ' Nokia ', ' Price ' =>1050), Array (' name ' = ' "laptop ', ' brand ' = ') ' Lenovo ', ' Price ' =>4300), Array (' name ' = = ' razor ', ' brand ' = ' Philips ', ' Price ' =>3100), Array (' name ' = ' "' Treadmill ', ' Brand ' = ' three and the Pine stone ', ' price ' =>4900, Array (' name ' = ' watch ', ' brand ' = ' casio ', ' Price ' =>960), Array (' name ' = = ' LCD tv ', ' Brand ' + ' sony ', ' Price ' =>6299, Array (' name ' = ' laser printer ', ' brand ' = ' hp ', ' Price ' =>1200)); $ Shoppinglist = Array_sort ($array, ' price ');p Rint_r ($ShoppingList);
The
Above is the $array of the two-dimensional array according to ' Price ' from low to high sort.