PHP multidimensional Array ordering Array_multisort () ____php
Last Update:2018-07-24
Source: Internet
Author: User
Array_multisort (Array_column ($tpldata,
' Time_stamp ' ),
Sort_desc , $tpldata); Sort_desc Descending; Sort_asc Ascending
time_stamp sort Field
multidimensional array sorting array_multisort ()
using PHP to sort with array_multisort functions <?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 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: Copy code Code as follows: Array ( [0] => array &nb sp;   ( [Volume] => [Edition] => 2 ) [1] => Array ( [Volume] => [ Edition] => 1 [2] => Array ( [Volume] => 86 [Edition] => 6 ) [3] => Array ( [Volume] => [Edition] => 6 ) [4] => Array ( [Volume] => [ Edition] => 2 [5] => Array ( [volume] => 67 [Edition] => 7 ) about Array_multisort official documentation also has a more detailed description: http://www.php.net/manual/zh/function.array-multisort.php
Two, custom function sort 1 Copy code 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); //Get 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 According to the specified key array * Param ARRAY $arr the array that will be sorted * @param string $keys Specifies the key * @param string $type sort type ASC | Desc * @return array / function ArraySort ($arr, $ Keys, $type = ' asc ') { $keysvalue = $new _array = Array (); &NBSP;&NBSP;&NBSP;&N bsp; foreach ($arr as $k => $v) { $keysvalue [$k] = $v [$keys]; } $type = = ' ASC '? Asort ($keysvalue): Arsort ($keysvalue); Reset ($keysvalue); foreach ($keysvalue as $k => $v) { & nbsp; $new _array[$k] = $arr [$k]; } return $new _array; }?>
Output Result: Copy code code as follows: Array ( [3] => Array ( &nbs p; [Volume] => [Edition] => 2 ) [4] = > Array ( [Volume] => [Edition] => 6 & nbsp; ) [1] => Array ( [volume] => [Edition] => 1 ) [2] => Array ( [Volume] => [Edition] => 6 ) [5] => Array ( [Volume] => [Edition] = > 7 ) [0] => Array & nbsp; ( [volume] => [Edition] => 2 ) One of the differences 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; and system function Array_multisort can be one-time to multiple keys and can specify more than one collation, system function is quite powerful, recommend the use of system functions, BI