PHP multi-dimensional array sorting implementation code

Source: Internet
Author: User

Sort by single array

Sort functions in ascending order

Bool sort (array & $ array [, int $ sort_flags = SORT_REGULAR])

The code is as follows: Copy code

<? Php
$ Fruits = array ("lemon", "orange", "banana", "apple ");
Sort ($ fruits );
Var_dump ($ fruits );
?>

-Rsort sort in descending order --

 

The code is as follows: Copy code

<? Php

$ Fruits = array ("lemon", "orange", "banana", "apple ");
Rsort ($ fruits); var_dump ($ fruits );
?>

For example, there is a multi-dimensional array:

 

The code is as follows: Copy code
$ 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 (),


Array_multisort () requires an array containing columns, so use the following code to retrieve the columns and then sort them.

The code is as follows: Copy code
// Retrieve the column list
Foreach ($ data as $ key => $ row ){
$ Volume [$ key] = $ row ['Volume '];
$ Edition [$ key] = $ row ['version'];
}

// Sort data by volume in descending order and edition in ascending order
// Use $ data as the last parameter and sort it by a common key
Array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data );

The data set is sorted as follows:
Volume | edition
------- + --------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7


// Custom function:
 

The code is as follows: Copy code
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
 
 

The code is as follows: Copy code
Print_r (multi_array_sort ($ arr, 'age '));
// 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
        )
 
)

In fact, there are still many ways to use in sorting, such as the arsort (), asort (), ksort (), krsort (), natsort (), natcasesort (), rsort (), usort (), array_multisort (), and uksort ().

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.