Detailed _php techniques for in-depth array multisort sorting principles

Source: Internet
Author: User
Copy Code code as follows:

<?php
$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);
?>

Copy Code code as follows:

<?php
$a = array (1,2,3);
$b = Array (3);
Arranges data in descending order according to volume, sorted by edition ascending order
Sort the $data as the last parameter in a common key
Array_multisort ($a, $b, $data);//Array One dimension number is different
Var_dump ($data);
?>

The above test code hints at the number of inconsistent arrays
<b>warning</b&gt: Array_multisort (): Array sizes are inconsistent in <b>g:\www\test\index.php</b > On line <b>15</b><br/>
Look again
Copy Code code as follows:

<?php
$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, 3, 4,4);//The number of two dimensions is different
?>

Copy Code code as follows:

<?php
$a = array (11,2,3,4,5,6);
$b = Array (3,3,3,3,3,3);
From the result, we see that the array (67,7,3,4,4) corresponding to 11 appears in the same order;
Arranges data in descending order according to volume, sorted by edition ascending order
Sort the $data as the last parameter in a common key
Array_multisort ($a, $b, $data);
Var_dump ($a, $data);
?>

from the results above you can tell:
Array parameters must have the same number of one-dimensional dimensions;
then each array corresponds to the position ( note not the same key, but from the natural position, such as $a (1=>4), corresponding to $b (99=>4) 4, because their position is the first, not the key ( 1,99), the corresponding position in the value of the same as wearing the same on each other equal bamboo poles, wearing one of the above values need to adjust position, it will cause the same "bamboo rod" other values appear vertical movement.

class:
$a $b $c
4=>7 8=>10 ' 999 ' => 0
9=>9 0=>1 999=>9
0=> 2 9=> 3 9999=>7
----------------------
If appear presses $a 7 and 9 to swap, will also drive $b 10 and 1 swap $c 0 and 9.
So the relationship is like the top three arrays, the same color on the same "front", and one of them, we want to change positions 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.