How do multidimensional arrays sort by a key?

Source: Internet
Author: User
Such as:
$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 ');

To rearrange the descending order by ID array


Reply to discussion (solution)

Self-solved ... Scattered points

foreach ($arr as $key + $row) {$cs [$key] = $row [' CS ']; $val [$key] = $row [' Val '];} Array_multisort ($cs, Sort_desc, $val, SORT_ASC, $arr);

Still have something to ask, use this method to solve, do not quite understand array_multisort this function of working method

Array_multisort requires that the number of first dimensions of the array participating in the sort be consistent
Array_multisort priority from left-to-right when sorting
Array_multisort the corresponding data item of the array participating in the sort at the same time

Array_multisort can be achieved.

First, look at the simplest case. There are two of arrays:

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

Array_multisort ($arr 1, $arr 2);

Print_r ($arr 1); The order to get is 1,5,9
Print_r ($arr 2); The order to get is 6,4,2

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

Let's add one more array to see what happens:

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

Array_multisort ($arr 1, $arr 2, $arr 3);

To view the results, 1 corresponds to 6 corresponding to 3, and so are the other items. This correspondence is what is called in the manual "preserve the original Key name association".

Alternatively, you can think of each array as a column of the database table. and the corresponding 1,6,3 is a data row, 9,2,7 for another data row ...

Array_multisort is sorted by the first array (pictured as a column), sorted by the second array (column) if the value of the first array (column) is the same.

You can use the following program to test:

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

Array_multisort ($arr 1, $arr 2, $arr 3);

You can imagine the result of $ARR3 here is (3,8,0,7).

Second, the next explanation of array_multisort parameters. The parameters of this function are very flexible. The simplest case is that, as shown above, with 1 or n arrays as parameters, it is important to note that the number of items in each array is the same, otherwise warning will cause the sort to fail.

Like this array_multisort ($arr 1, $arr 2, $arr 3); By default, all arrays are sorted in ascending order, and if you want to $arr2 in descending order and compare them as strings, write:

Array_multisort ($arr 1, $arr 2, Sort_desc, sort_string, $arr 3);

Each array can be followed by a sort order flag or a sort type flag, or both flags appear at the same time. However, each sort flag can only appear after each array.

Details are as follows:

Sort order Flags:

Sort_asc-Sort by ascending order (default)

Sort_desc-Sort by descending order

? Test: Http://zhidao.baidu.com/question/213141342.html

$ids = Array ();
foreach ($result as $j) {
$ids [] = $j [' Rid '];
}
Array_multisort ($ids, Sort_desc, $result); Sort

  • 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.