This article mainly introduces three methods of PHP 2D array sorting and user-defined functions. For more information, see
This article mainly introduces three methods of PHP 2D array sorting and user-defined functions. For more information, see
Generally, sorting is performed by sorting the database or nosql (eg: redis) and then outputting it to the program. However, sometimes we need to sort the array directly through PHP, objects and arrays are the most used for storing data in PHP, but arrays are the most used for processing, because there are rich built-in function libraries (in fact, objects can also be understood as arrays to a certain extent), these function libraries can greatly help us implement some functions. Common system functions include sort, asort, arsort, ksort, and krsort. Here I mainly talk about sorting two-dimensional arrays. There are two methods:
I. sort by using the PHP built-in array_multisort Function
The Code is as follows:
$ 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
)
)
The official documents of array_multisort are also described in detail:
Ii. Sorting by user-defined functions 1
The Code is as follows:
$ 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'];
}
$ Ret = arraySort ($ data, 'Volume ', 'desc ');
Print_r ($ ret );
/**
* @ Desc arraySort php two-dimensional array sorting sorts the array according to the specified key
* @ Param array $ array to be sorted by arr
* @ Param string $ keys specifies the sorted key
* @ Param string $ type sorting 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:
The Code is as follows: