PHP multi-dimensional array sorting based on an item in a two-dimensional array _ PHP Tutorial

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

Related Article

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.