Simple Example of sorting two-dimensional arrays by php
This article mainly introduces a simple example of php sorting two-dimensional arrays. If you need it, please refer to it.
This article describes how to use the array_multisort function in php to sort two-dimensional arrays. For more information, see. Following the previous article: PHP two-dimensional array sorting custom function, today we will introduce another example of php two-dimensional array sorting.
The sorting of two-dimensional arrays in php is very simple. The array_multisort function is mainly used.
Example:
The Code is as follows:
<? Php
/**
* Php 2D array sorting
* Edit www.jb51.net
*/
$ Data = array ();
$ Data [] = array ('Volume '=> 67, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 1 );
$ Data [] = array ('Volume '=> 85, 'version' => 6 );
$ Data [] = array ('Volume '=> 98, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 6 );
$ Data [] = array ('Volume '=> 67, 'version' => 7 );
// Retrieve the column list
Foreach ($ data as $ key => $ row)
{
$ Volume [$ key] = $ row ['Volume '];
$ Edition [$ key] = $ row ['version'];
}
Array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data );
Print_r ($ data );
?>
Output result:
The Code is as follows:
Array
(
[0] => Array
(
[Volume] => 98
[Edition] => 2
)
[1] => Array
(
[Volume] => 86
[Edition] => 1
)
[2] => Array
(
[Volume] => 86
[Edition] => 6
)
[3] => Array
(
[Volume] => 85
[Edition] => 6
)
[4] => Array
(
[Volume] => 67
[Edition] => 2
)
[5] => Array
(
[Volume] => 67
[Edition] => 7
)
)
Note:
The parameters of the array_multisort function are flexible. For details, refer to the instructions in the php manual.