PHP built-in function array_multisort requires the same size as each array
$areas is a two-dimensional array of regions, containing the number of people and the number of times, which are now sorted in descending order of these 2 numbers
Copy the Code code 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, there are more than one array:
Copy the Code code 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 ' = ' = ', ' age ' = 5),
' c ' = = Array (' id ' = = 1, ' name ' = + 2, ' age ' = 2)
);
You need to sort the age items in a two-dimensional array.
PHP's built-in function, Array_multisort (), can be used to read the manual.
Custom functions:
Copy the Code code 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;
}
Processing
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
The above describes the array of PHP multidimensional array sorting problem according to an item in a two-dimensional array, including the contents of the array, I hope that the PHP tutorial interested in a friend helpful.