This article mainly introduces three methods of PHP two-dimensional array sorting and user-defined functions. For more information about sorting, see databases or nosql (eg: redis) sort the order first and then output it to the program for direct use. However, sometimes we need to sort the array directly through PHP. in PHP, objects and arrays are the most used to store data, however, an array is used for processing, because there are a wealth of built-in function libraries (in fact, objects can also be understood as arrays to a certain extent ), these function libraries can help us implement some functions to a large extent. 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
)
)
About array_multisort official documentation also has a more detailed description: http://www.php.net/manual/zh/function.array-multisort.php
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: Array
(
[3] => Array
(
[Volume] => 98
[Edition] => 2
)
[4] => Array
(
[Volume] => 86
[Edition] => 6
)
[1] => Array
(
[Volume] => 86
[Edition] => 1
)
[2] => Array
(
[Volume] => 85
[Edition] => 6
)
[5] => Array
(
[Volume] => 67
[Edition] => 7
)
[0] => Array
(
[Volume] => 67
[Edition] => 2
)
)
The difference between this UDF and the system function is that the UDF only supports sorting of a specific key. if you want to support sorting of multiple keys, you need to execute it multiple times; the system function array_multisort can specify multiple sorting rules for multiple keys at a time. the system function is quite powerful. we recommend that you use system functions. after all, it is implemented at the underlying level of C, here is just an example to illustrate how to sort the array through a user-defined function. of course, this user-defined function can be expanded to support more sorting rules. It is used in many scenarios, such as rankings, rankings, and scores.
III. sorting of user-defined functions 2
The following function sorts a given two-dimensional array by the specified key value. First, let's look at the function definition:
The code is 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 ($ keysvalue );
} Else {
Arsort ($ keysvalue );
}
Reset ($ keysvalue );
Foreach ($ keysvalue as $ k => $ v ){
$ New_array [$ k] = $ arr [$ k];
}
Return $ new_array;
}
It can sort two-dimensional arrays by specified key values, or specify the ascending or descending sort method (the default is ascending). Usage example:
The code is as follows:
$ Array = array (
Array ('name' => 'phone', 'brand' => 'Nokia ', 'price' => 1050 ),
Array ('name' => 'laptop ', 'brand' => 'Lenovo', 'price' => 4300 ),
Array ('name' => 'shares', 'brand' => 'Phillips', 'price' => 3100 ),
Array ('name' => 'Treadmill ', 'brand' => 'three', 'price' => 4900 ),
Array ('name' => 'Watch ', 'brand' => 'case', '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 );
The above orders the two-dimensional array $ array in descending order of 'price.