PHP Multidimensional array sort function array_multisort () example explanation

Source: Internet
Author: User
Tags array sort arrays

first, see the simplest situation.

There are two 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 are always corresponding: 1 corresponds to the 6,9 corresponding 2,5 4. Let's add one more array and 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);

Looking at the results, 1 corresponds to 6 for 3 from start to finish, as are the other items. This correspondence is the so-called "preserve the original key-name association" in the Manual.

Alternatively, you can think of each array as a column in a database table. The corresponding 1, 6, 3 is a data row, 9, 2, 7 for another row of data, 5, 4, 8 another row.

Array_multisort the first array (imagined 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 procedure 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);

It can be imagined that the result of $ARR3 here is (3, 8, 0, 7).

Second, then explain the parameters of Array_multisort.

The parameters of this function are very flexible. The simplest scenario is as an argument with 1 or n arrays as shown above, and it is important to note that the number of items in each array is the same, otherwise warning will result in a sort failure.

Like this array_multisort ($arr 1, $arr 2, $arr 3); By default, all arrays are in ascending order, and if you want to $arr2 descending, 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 two flags at the same time. However, each sort flag can only appear after each array.

Details are as follows:

Sort order Flags:

SORT_ASC-Sort in ascending order (default)
Sort_desc-Sort in descending order

Sort Type Flags:

Sort_regular-Compare items in the usual way (default)
Sort_numeric-Compare items by numerical comparison
Sort_string-Comparing items by string



third, the last is Array_multisort have any practical effect.

We usually have some multidimensional arrays that need sorting:

$guys = Array (
Array
' Name ' => ' Jake ',
' Score ' => 80,
' Grade ' => ' A '
),
Array
' Name ' => ' Jin ',
' Score ' => 70,
' Grade ' => ' A '
),
Array
' Name ' => ' John ',
' Score ' => 80,
' Grade ' => ' A '
),
Array
' Name ' => ' Ben ',
' Score ' => 20,
' Grade ' => ' B '
)
);

For example, we want to arrange by the order of the results, if the results are the same in ascending order of the name.

Then we need to get two more arrays in 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);

On the line.

Can you be more flexible, you want to order each time to get some other array out?

You can actually encapsulate several functions:

<?php
/**
* Sort the array according to the specified key
*
Usage
* <code>
* $rows = Array (
Array (' ID ' => 1, ' value ' => ' 1-1 ', ' parent ' => 1),
Array (' ID ' => 2, ' value ' => ' 2-1 ', ' parent ' => 1),
Array (' ID ' => 3, ' value ' => ' 3-1 ', ' parent ' => 1),
Array (' ID ' => 4, ' value ' => ' 4-1 ', ' parent ' => 2),
Array (' ID ' => 5, ' value ' => ' 5-1 ', ' parent ' => 2),
Array (' ID ' => 6, ' value ' => ' 6-1 ', ' Parent ' => 3),
);
*
* $rows = List_sortbycol ($rows, ' id ', SORT_DESC);
* Dump ($rows);
*//The output result is:
*//Array (
*//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 ' => 1, ' value ' => ' 1-1 ', ' parent ' => 1),
* // )
* </code>
*
* @param array $array to sort
* @param string $keyname the sorted key
* @param int $dir sort Direction
* Array sorted @return Array
*/
function List_sortbycol ($array, $keyname, $dir = SORT_ASC)
{
Return List_sortbymulticols ($array, Array ($keyname => $dir));
}

/**
* Sort a two-dimensional array by multiple columns, similar to the order by in SQL statements
*
Usage
* <code>
* $rows = List_sortbymulticols ($rows, Array (
' Parent ' => SORT_ASC,
' Name ' => Sort_desc,
* ));
* </code>
*
* @param array $rowset arrays or rowsets to sort
* @param array $args sort keys
*
* Array sorted @return Array
*/
function List_sortbymulticols ($rowset, $args)
{
$sortArray = Array ();
$sortRule = ';

foreach ($args as $sortField => $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;
}



php two-dimensional array ordering (Sort_array)

/**
* Sort a two-dimensional array
* Analog data table records sorted by field
*
* <code>
* @list_sort ($list, $get [' Orderkey '], $get [' OrderType ']);
* </code>
* @param array $array to sort
* @param string $orderKey sort key/Field
* @param string $orderType sort, ' asc ': Ascending, ' desc ': Descending
* @param string $orderValueType sort field value type, ' number ': digit, ' string ': string
*/
Function List_sort (& $array, $orderKey, $orderType = ' asc ', $orderValueType = ' string ')
{
if (Is_array ($array))
{
$ORDERARR = Array ();
foreach ($array as $val)
{
$ORDERARR [] = $val [$orderKey];
}
$orderType = ($orderType = = ' ASC ')? Sort_asc:sort_desc;
$orderValueType = ($orderValueType = = ' string ')? Sort_string:sort_numeric;
Array_multisort ($ORDERARR, $orderType, $orderValueType, $array);
}
}

Application:

@sort_array ($list, $get [' sort '], $get [' Order '], "string");

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.