Home >Backend Development >PHP Tutorial >Detailed explanation of array multisort sorting principle_PHP tutorial

Detailed explanation of array multisort sorting principle_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:05:26926browse

Copy code The code is as follows:

$data[] = array('volume' => 67 , 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array ('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);
?> ;

Copy code The code is as follows:

$a = array (1,2,3);
$b = array(3);
// Arrange the data in descending order according to volume, and in ascending order according to edition
// Use $data as the last parameter to generalize Key sorting
array_multisort($a, $b, $data);//Arrays with different one-dimensional numbers
var_dump( $data);
?>

The above test code prompts that the array sizes are inconsistent in G:wwwtestindex.php on line 15

Look again

Copy code The code is as follows :
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array ('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data [] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7, 3, 4,4);//The number of two-dimensional numbers is different
?>



Copy code The code is as follows:
$a = array(11,2,3 ,4,5,6);
$b = array(3,3,3,3,3,3);
//From the result, we can see the array(67,7,3, corresponding to 11, 4,4) appeared in the same order;
// Sort the data in descending order according to volume and in ascending order according to edition
// Use $data as the last parameter and sort by common key
array_multisort($a , $b, $data);
var_dump( $a,$data);
?>


From the above results we can know:
The array parameters must have the same one-dimensional number;
Then the corresponding position of each array
(Note that it is not the same key, but corresponding from the natural position, such as $a( 1=>4), corresponding to 4 of $b(99=>4), because their positions are all the first, not the corresponding relationship of key(1,99) ), The value in the corresponding position is like being threaded on equal bamboo poles. When one of the values ​​on it needs to be adjusted, it will cause other values ​​on the same "bamboo pole" to move vertically. .
Class:
$a $b $c
4=>7 8=>10 '999' => 0
9=>9 0=>1 999=>9
0=> 2 9=> 3 9999=>7
------------- --------
If the 7 and 9 of $a are swapped, it will also cause the 10 and 1 of $b to be swapped with the 0 and 9 of $c.
So the relationship is Like the three arrays above, those of the same color are on the same "front". If one of them changes, everyone must change positions together.


http://www.bkjia.com/PHPjc/327698.html

truehttp: //www.bkjia.com/PHPjc/327698.htmlTechArticleCopy the code as follows: ?php $data[] = array('volume' = 67, 'edition' = 2); $data[] = array('volume' = 86, 'edition' = 1); $data[] = array('volume' = 85, 'edition' = 6); $data[]...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn