How to sort multi-dimensional arrays by a key

Source: Internet
Author: User
How to sort multi-dimensional arrays by a key? For example: $ arrarray (); $ arr [] array (id & gt; 88, val & gt; xxx); $ arr [] array (id & gt; 188, val & gt; xxx); $ arr [] array (id & gt; 81, val & gt; xxx ); $ arr [] array (how to sort the id & gt multi-dimensional array by a key?
For example:
$arr=array();
$arr[]=array('id'=>88,'val'=>'xxx');
$arr[]=array('id'=>188,'val'=>'xxx');
$arr[]=array('id'=>81,'val'=>'xxx');
$arr[]=array('id'=>388,'val'=>'xxx');

You want to sort the array in descending order by id.
------ Solution ----------------------
Array_multisort requires that the number of the first dimension of the array to be sorted be consistent.
Array_multisort is sorted by priority from left to right.
Array_multisort exchanges the corresponding data items in the sorted array at the same time.

------ Solution ----------------------
Array_multisort can be implemented.
------ Solution ----------------------
I. first look at the simplest situation. There are two arrays:

$ Arr1 = array (1, 9, 5 );
$ Arr2 = array (6, 2, 4 );

Array_multisort ($ arr1, $ arr2 );

Print_r ($ arr1); // The order is, 9.
Print_r ($ arr2); // The order is 6, 4, 2.

I estimate that the values of the two arrays correspond from beginning to end: 1 corresponds to 6, 9 corresponds to 2, 5 corresponds to 4.

Let's add an array to see what will happen:

$ Arr1 = array (1, 9, 5 );
$ Arr2 = array (6, 2, 4 );
$ Arr3 = array (3, 7, 8 );

Array_multisort ($ arr1, $ arr2, $ arr3 );

View the result. 1 corresponds to 6 and 3 from start to end. this is also true for other items. This correspondence is the so-called "keep the original key-name association during sorting" in the manual ".

You can also think of each array as a column in a database table. The 1, 6, and 3 corresponding to a data row, 9, 2, and 7 are another data row...

Array_multisort will first sort by the first array (as expected). If the value of the first array (column) is the same, it will sort by the second array (column.

You can use the following program to test the function:

$ Arr1 = array (1, 9, 5, 9 );
$ Arr2 = array (6, 2, 4, 1 );
$ Arr3 = array (3, 7, 8, 0 );

Array_multisort ($ arr1, $ arr2, $ arr3 );

The result of $ arr3 is ).

II. The following describes the parameters of array_multisort. The parameters of this function are flexible. The simplest case is that one or n arrays are used as parameters as shown above. Note that the number of items in each array must be the same; otherwise, the sorting will fail due to warning.

Array_multisort ($ arr1, $ arr2, $ arr3); by default, all arrays are sorted in ascending order. if you want to sort $ arr2 in descending order and compare it as a string, you must write it as follows:

Array_multisort ($ arr1, $ arr2, SORT_DESC, SORT_STRING, $ arr3 );

Each array can be followed by a sort order sign or a sort type sign, or both. However, each sort flag can only appear after each array.

The details are as follows:

Sort order mark:

SORT_ASC-sort by ascending order (default)

SORT_DESC-sort by descent

Exam: http://zhidao.baidu.com/question/213141342.html
------ Solution ----------------------
$ Ids = array ();
Foreach ($ result as $ j ){
$ Ids [] = $ j ['rid'];
}
Array_multisort ($ ids, SORT_DESC, $ result); // sort

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.