Ordering of arrays of PHP multidimensional arrays sorted by an item in a two-dimensional array

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.