In-depth explanation of array multisort sorting principles

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php
$ Data [] = array ('Volume '=> 67, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 1 );
$ Data [] = array ('Volume '=> 85, 'version' => 6 );
$ Data [] = array ('Volume '=> 98, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 6 );
$ Data [] = array ('Volume '=> 67, 'version' => 7 );
?>

Copy codeThe Code is as follows:
<? Php
$ A = array (1, 2, 3 );
$ B = array (3 );
// Sort data by volume in descending order and edition in ascending order
// Use $ data as the last parameter and sort it by a common key
Array_multisort ($ a, $ B, $ data); // The number of arrays in one dimension is different.
Var_dump ($ data );
?>

The above test code prompts the number of inconsistent Arrays
<B> Warning </B>: array_multisort (): Array sizes are inconsistent in <B> G: \ www \ test \ index. php </B> on line <B> 15 </B> <br/>
Check again
Copy codeThe Code is as follows:
<? Php
$ Data [] = array ('Volume '=> 67, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 1 );
$ Data [] = array ('Volume '=> 85, 'version' => 6 );
$ Data [] = array ('Volume '=> 98, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 6 );
$ Data [] = array ('Volume '=> 67, 'version' => 7, 3, 4, 4); // two-dimensional numbers are different.
?>

Copy codeThe Code is as follows:
<? Php
$ A = array );
$ B = array (3, 3, 3, 3, 3 );
// The array (, 7, 3, 4) corresponding to 11 appears in the same order;
// Sort data by volume in descending order and edition in ascending order
// Use $ data as the last parameter and sort it by a common key
Array_multisort ($ a, $ B, $ data );
Var_dump ($ a, $ data );
?>

The above results show that:
The array parameters must have the same number of one dimensions;
Then the corresponding position of each array (note that it is not the same key, but corresponds to a natural location, for example, $ a (1 => 4), corresponding to $ B (99 => 4) because they are in the first position, rather than the key () ing), the values in the corresponding positions are the same as those in the equal bamboo, when one of the preceding values needs to be adjusted, other values on the same bamboo are moved vertically.
 
Class:
$ A $ B $ c
4 => 7 8 => 10 '000000' => 0
9 => 9 0 => 1 999 => 9
0 => 2 9 => 3 9999 => 7
----------------------
If you change the value by 7 and 9 of $ a, the 10 and 1 of $ B will change to 0 and 9 of $ c.
So the relationship is like the above three arrays, with the same color on the same "Front", one of which is changed, and everyone needs to change the location together.

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.