The sorting of PHP multi-dimensional arrays is based on an item in a two-dimensional array. The PHP built-in function array_multisort requires that each array be of the same size $ areas is a two-dimensional array of the region, including the number and number of times, now we need to sort the code in descending order according to these two types of data. for example, the PHP built-in function array_multisort requires the same size for each array.
$ Areas is a two-dimensional array of regions, including the number of people and the number of times. now we need to sort these two numbers in descending order.
The code is as follows:
Foreach ($ areaArray as & $ areas ){
$ Times = $ numbers = array ();
Foreach ($ areas as $ province => $ v ){
$ Times [$ province] = $ v ['Times '];
$ Numbers [$ province] = $ v ['numbers '];
}
Array_multisort ($ times, SORT_DESC, $ numbers, SORT_DESC, $ areas );
}
For example, most of them are arrays:
The code is as follows:
$ Arr = array (
'D' => array ('id' => 5, 'name' => 1, 'age' => 7 ),
'B' => array ('id' => 2, 'name' => 3, 'age' => 4 ),
'A' => array ('id' => 8, 'name' => 10, 'age' => 5 ),
'C' => array ('id' => 1, 'name' => 2, 'age' => 2)
);
Sort age items in a two-dimensional array.
You need to use the PHP built-in function array_multisort (). you can refer to the manual.
Custom functions:
The code is as follows:
Function multi_array_sort ($ multi_array, $ sort_key, $ sort = SORT_ASC ){
If (is_array ($ multi_array )){
Foreach ($ multi_array as $ row_array ){
If (is_array ($ row_array )){
$ Key_array [] = $ row_array [$ sort_key];
} Else {
Return false;
}
}
} Else {
Return false;
}
Array_multisort ($ key_array, $ sort, $ multi_array );
Return $ multi_array;
}
// Process
Echo"
";
Print_r (multi_array_sort ($ arr, 'age'); exit;
// Output
Array
(
[C] => Array
(
[Id] => 1
[Name] => 2
[Age] => 2
)
[B] => Array
(
[Id] => 2
[Name] => 3
[Age] => 4
)
[A] => Array
(
[Id] => 8
[Name] => 10
[Age] => 5
)
[D] => Array
(
[Id] => 5
[Name] => 1
[Age] => 7
)
)
Written by Daewoo
0
Array_multisort requires that each array be of the same size $ areas is a two-dimensional array of the region, including the number of people and number of times. now we need to sort the code in descending order according to these two types of numbers, as shown in...