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.
Copy codeThe 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:Copy codeThe 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:Copy codeThe 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 "<pre/> ";
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