An example of the sorting principle of array multisort in PHP

Source: Internet
Author: User
    1. $data [] = Array (' volume ' = +, ' edition ' = 2);
    2. $data [] = Array (' volume ' = +, ' edition ' = 1);
    3. $data [] = Array (' volume ' = =, ' edition ' = 6);
    4. $data [] = Array (' volume ' = = 98, ' edition ' = 2);
    5. $data [] = Array (' volume ' = +, ' edition ' = 6);
    6. $data [] = Array (' volume ' = +, ' edition ' = 7);
    7. ?>
Copy Code

Example 2:

    1. $a = Array (+/-);
    2. $b = Array (3);
    3. Sort data in descending order according to volume, in ascending order of edition
    4. The $data as the last parameter, sorted by the Universal key
    5. Array_multisort ($a, $b, $data);//array one-dimensional number is different
    6. Var_dump ($data);
    7. ?>
Copy Code

The above test code indicates the number of inconsistencies in the array. Warning:array_multisort (): Array sizes is inconsistent in G:\www\test\index.php on LINE15

Let's look at an example:

    1. $data [] = Array (' volume ' = +, ' edition ' = 2);
    2. $data [] = Array (' volume ' = +, ' edition ' = 1);
    3. $data [] = Array (' volume ' = =, ' edition ' = 6);
    4. $data [] = Array (' volume ' = = 98, ' edition ' = 2);
    5. $data [] = Array (' volume ' = +, ' edition ' = 6);
    6. $data [] = Array (' volume ' = +, ' Edition ' = 7, 3, +);//Two-dimensional number is different
    7. ?>
Copy Code

Code Listing 2:

    1. $a = array (11,2,3,4,5,6);
    2. $b = Array (3,3,3,3,3,3);
    3. From the results, we see that the corresponding 11 array (67,7,3,4,4) appears in the same order;
    4. Sort data in descending order according to volume, in ascending order of edition
    5. The $data as the last parameter, sorted by the Universal key
    6. Array_multisort ($a, $b, $data);
    7. Var_dump ($a, $data);
    8. ?>
Copy Code

As you can tell from the above results, the array parameter must have the same number of one-dimensional, and then the corresponding position of each array (note not the same key, but from the natural position, such as $ A (1=>4), corresponding to the $b (99=>4) 4, because their position is the first, not a key ( 1,99), the corresponding position of the value is like wearing on each other equal to the bamboo pole, wearing one of the above values need to adjust the position, it will cause the same "bamboo bar" on the other values appear vertical movement.

Class:

$a $b $c 4=>7 8=>10 ' 999 ' = 09=>9 0=>1 999=>90=> 2 9=> 3 9999=>7

If there is a $ A 7 and 9 swap, it will drive the $b 10 and 1 of the swap $c 0 and 9. So the relationship is like the above three arrays, the same color on the same "front", one of them, we have to change positions together.

Let's introduce these and hopefully help you understand the use of multisort array sorting.

>>> For more information, check out the PHP array sorting method Daquan <<<

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