PHP Multidimensional Array sort array_multisort function usage

Source: Internet
Author: User
Tags foreach array sort


Sorting 2-D arrays or multidimensional arrays is a common problem, in PHP we have a special multidimensional array sorting function, the following briefly:
Array_multisort (array1,sorting order, sorting type,array2,array3. is a function that sorts multiple arrays or multidimensional arrays.


The first argument is an array, and each subsequent argument may be an array, or it may be one of the following sort order flags (the sort flags are used to change the default arrangement order):
SORT_ASC-Default, in ascending order. (A-Z)
Sort_desc-in descending order. (Z-A)
You can then specify the sort type:
Sort_regular-Default. Arranges each item in a regular order.
Sort_numeric-Arranges each item in numerical order.
Sort_string-Alphabetical order of each item


Simple 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 of database tables. The corresponding 1,6,3 is a data row, 9,2,7 is another data 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 this $arr3 is (3,8,0,7).

Example

<?php
The use of PHP multidimensional array sorting array_multisort
$data [] = Array (' volume ' =>, ' Edition ' => 2);
$data [] = Array (' volume ' =>, ' edition ' => 1);
$data [] = Array (' volume ' =>, ' Edition ' => 6);
$data [] = Array (' volume ' =>, ' Edition ' => 2);
$data [] = Array (' volume ' =>, ' Edition ' => 6);
$data [] = Array (' volume ' =>, ' edition ' => 7);
To generate an array of sort fields
foreach ($data as $key => $row) {
$volume [$key] = $row [' volume '];
$edition [$key] = $row [' Edition '];
}

According to volume descending, edition ascending
Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);

Array after sort
Print_r ($data);
Exit

?>

Add: Take a look at two practical examples:

1. Sorting multiple arrays at once:


$num 1 = Array (3, 5, 4, 3);
$num 2 = Array (27, 50, 44, 78);
Array_multisort ($num 1, SORT_ASC, $num 2, Sort_desc);

Print_r ($num 1);
Print_r ($num 2);
Result:array ([0] => 3 [1] => 3 [2] => 4 [3] => 5) Array ([0] => [1] => [2] => [3] =&G T 50)


2. Sort the multidimensional Array (example of a two-bit array):

$arr = Array (
    ' 0 ' => array (
        ' num1 ' => 3,< br>         ' num2 ' =>
   ),
   
& nbsp;   ' 1 ' => array (
        ' NUM1 ' => 5,
         ' num2 ' =>
   ),
   
     ' 2 ' => array (
        ' NUM1 ' => 4,
         ' num2 ' =>
   ),
   
    ' 3 ' => arr Ay (
        ' NUM1 ' => 3,
        ' Num2 ' =>
   )
;

foreach ($arr as $key => $row) {
$num 1[$key] = $row [' NUM1 '];
$num 2[$key] = $row [' num2 '];
}

Array_multisort ($num 1, SORT_ASC, $num 2, Sort_desc, $arr);

Print_r ($arr);
Result:array ([0]=>array ([num1]=>3 [num2]=>78) [1]=>array [Num1]=>3] (num2]=>27 [2]=>Array] num1]=>4 [num2]=>44] [3]=>array ([Num1]=>5 [num2]=>50)]

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.