This article mainly introduces PHP two-dimensional array ordering 3 methods and custom function sharing, need friends can refer to the following
In general, we are sorted by database or NoSQL (Eg:redis) and then exported to the program for direct use. But there are times when we need to sort the arrays directly from PHP, while in PHP we use most of the objects and arrays to store the data. Because there are very rich built-in function libraries (in fact, the object to some extent can also be understood as an array), these libraries can largely help us achieve some functionality. Commonly used system functions are sort, asort, Arsort, Ksort, Krsort, and so on, here I would like to say that the two-dimensional array of the order, both methods: A, PHP with the Array_multisort function sort The code is as follows: <?php $data = array (); $data [] = Array (' volume ' =>, ' Edition ' => 2); $data [] = Array (' volume ' =>, ' edition ' => 1); $data [] = Array (' volume ' =>, ' Edition ' => 6); $data [] = Array (' volume ' =>, ' Edition ' => 2); $data [] = Array (' volume ' =>, ' Edition ' => 6); $data [] = Array (' volume ' =>, ' edition ' => 7); //Get column list foreach ($data as $key => $row) { &NBSP ; $volume [$key] = $row [' volume ']; $edition [$key] = $row [' Edition ']; }   Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data); Print_r ($data)?> OUTPUT Result: The code is as follows: Array ( [0] => Array &N Bsp ( , [volume] => [edi tion] => 2 ) [1] => Array ( &NBS P [volume] => [Edition] => 1 &N Bsp ) [2] => Array ( [Volume] => 86 [Edition] => 6 ) [3] => Array &N Bsp ( [volume] => &NBS P [Edition] => 6 ) [4] => Array ( [volume] => &N Bsp [Edition] => 2 ) [5] => Array &N Bsp ( [volume] => [Edition] = > 7 ) about Array_multisort official documentation also has a more detailed description: http://www.php.net/manual/zh/ function.array-multisort.php II, Custom function sort 1 code as follows: <?php $data = array (); $data [] = Array (' volume ' =>, ' Edition ' => 2); $data [] = Array (' volume ' =>, ' edition ' => 1); $data [] = Array (' volume ' =>, ' Edition ' => 6); $data [] = Array (' volume ' =>, ' Edition ' => 2); $data [] = Array (' volume ' =>, ' Edition ' => 6); $data [] = Array (' volume ' =>, ' edition ' => 7); &nbsP //Get column list foreach ($data as $key => $row) { $volu me[$key] = $row [' volume ']; $edition [$key] = $row [' Edition ']; } $ret = ArraySort ($data, ' volume ', ' desc '); Print_r ($ret); /** * @desc arraysort php two-dimensional array sorting sorted by specified key array * @param arr Ay $arr the array to 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]; { &NB Sp $type = ' ASC '? Asort ($keysvalue): Arsort ($Keysvalue); Reset ($keysvalue); foreach ($keysvalue as $k => $v) { $new _array[$k] = $arr [$k]; } return $new _array; }?> output: code as follows: Array ( [3] => array ( & nbsp [volume] => [Edition] => 2 &N Bsp ) [4] => Array ( [vol. Ume] => [Edition] => 6 ) &NBS P [1] => Array ( [volume] =>- &N Bsp [Edition] => 1 ) [2] => ARray ( [volume] => [Edition] => 6 ) [5] => Array & nbsp ( [volume] => [Edition] => 7 ) [0] => Array ( &NBS P [volume] => [Edition] => 2 ) The difference between this custom function and the system function is that the custom function only supports sorting for a key, and it needs to be executed multiple times to support the ordering of multiple key keys; The system function Array_multisort can be one-time to multiple key and can specify multiple collation, system function is still quite powerful, recommend the use of system functions, after all, is the C-level implementation, here is just an example of a custom function to order the array, Of course, this custom function can also continue to expand to support more collations. In the rankings, rankings, scores and other scenes used in a lot of. Three, custom function sort 2 The following function is to sort a given two-dimensional array by the specified key value, first look at the function definition: Code as follows: function Array_sort ($arr, $keys, $type = ' ASC ') { $keysvalUE = $new _array = Array (); foreach ($arr as $k => $v) { $keysvalue [$k] = $v [$keys];  } if ($type = = ' asc ') { Asort ($keys Value); }else{ Arsort ($keysvalue); } reset ($keysvalue); foreach ($keysvalue as $k => $v) { $new _array[$k] = $arr [$k]; } return $new _array; } It can sort a two-dimensional array by the specified key value, or you can specify ascending or descending sort (the default is ascending), using the example: The code is as follows: $array = Array ( array (' name ' => ' cell phone ', ' brand ' => ' Nokia '), ' Price ' =>1050, array (' name ' => ' laptop ', ' brand ' => ' Lenovo ', ' Price ' =>4300), array (' name ' = > ' razor ', ' Brand ' => ' Philips ', ' Price ' =>3100, array (' name ' => ' running machine ', ' brand ' => ' three and 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 '); Print_r ($ShoppingList); Above is the $array of this two-dimensional array according to the ' PRICe ' sort from low to high.