Array_multisort multi-dimensional array sorting usage full solution

Source: Internet
Author: User
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);p Rint_r ($arr 1); The order obtained is 1,5,9print_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);

Output:

Array ([0] = 1 [1] = 5 [2] = 9 [3] = 9) Array ([0] = 6 [1] = 4 [2] = 1 [3] + 2) Array ([0] = 3 [1] = 8 [2] = 0 [3] = 7)

Here we find that the $ARR1 has been sorted in ascending order. But $ARR2 and $ARR3 are not sorted.

But when you look at it, it turns out that each of the 3 arrays has one by one corresponding to each element.


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

Sort Type flag:

Sort_regular-Compare items by usual method (default)

Sort_numeric-Compare items by value

Sort_string-Compare items by string

Third, the last is array_multisort have what practical function.

We usually have some multidimensional arrays that need to be sorted:

$guys = Array (    [0] = = Array    (        [name] + Jake        [score] =        [Grade] = A    )         [1] =&G T Array    (        [name] = Jin        [score] = [        grade] = A    )         [2] = = = Array    (        [ Name] = John        [score] [        grade] + A    )         [3] = = Array    (        [name] = Ben
  [score]        [grade] (= B    ))

For example, we want to rank in reverse order, if the results are the same, in ascending order by name.
Then we need to get two more arrays according to the Order of $guys:

$scores = Array (80,70,80,20); $names = Array (' Jake ', ' Jin ', ' John ', ' Ben ');

And then

Array_multisort ($scores, Sort_desc, $names, $guys);

It's all right.
Can you be more flexible and have to get some extra arrays every time you want to sort?
In fact, in the Qeephp Helper_array class has been encapsulated very well, the following is its two methods, the need for people to modify their own can be used:

/*** sorts the array according to the specified key * * Usage: * @code php* $rows = Array (* Array (' id ' = = 1, ' value ' = ' 1-1 ', ' parent ' = = 1), * Array (' ID ')  = 2, ' value ' = ' 2-1 ', ' parent ' and ' = 1 ', * Array (' id ' = 3, ' value ' = ' 3-1 ', ' parent ' = 1), * Array (' ID ')  = 4, ' value ' = ' 4-1 ', ' parent ' and ' = 2 ', * Array (' id ' = 5, ' value ' = ' 5-1 ', ' parent ' = 2), * Array (' ID ') = 6, ' value ' = ' 6-1 ', ' parent ' = 3), *), * * $rows = Helper_array::sortbycol ($rows, ' id ', SORT_DESC); * Dump ($ro  WS); *//output result is: *//Array (' id ' = = 6, ' value ' = ' 6-1 ', ' parent ' = + 3), *//Array (' id ' = 5, ' value ') = ' 5-1 ', ' parent ' = 2), *//Array (' id ' = + 4, ' value ' = ' 4-1 ', ' parent ' = 2), *//Array (' id ' = 3, ' Value ' = ' = ' 3-1 ', ' parent ' = 1), *//Array (' id ' = + 2, ' value ' = ' 2-1 ', ' parent ' = 1), *//Array (' ID ' =&gt ; 1, ' value ' = ' 1-1 ', ' parent ' = 1), *//) * @endcode * * @param array $array to sort by * @param string $keyname sort the key * @par AM int $dir Sort direction * * @reTurn array sorted by */static function Sortbycol ($array, $keyname, $dir = sort_asc) {return Self::sortbymulticols ($array, Array ($keyname = $dir));}

/*** sorts a two-dimensional array in multiple columns, similar to the order by** usage in the SQL statement: * @code php* $rows = Helper_array::sortbymulticols ($rows, Array (* ' parent ')  = = SORT_ASC, * ' name ' = sort_desc,*)); * @endcode * * @param array $rowset to sort the arrays * @param array $args sorted keys * * @return Array sorted by */static function Sortbymulticols ($rowset, $args) {$sortArray = array (); $sortRule = "; foreach ($args as $sort Field = $sortDir) {foreach ($rowset as $offset = = $row) {$sortArray [$sortField] [$offset] = $row [$sortField];} $sortRule. = ' $sortArray [\ '. $sortField. '\'], ' . $sortDir. ', ';} if (Empty ($sortArray) | | empty ($sortRule)) {return $rowset;} Eval (' Array_multisort ('. $sortRule. ' $rowset); return $rowset;}
  • 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.