Array_multisort () This function can sort multiple php arrays , with the result that all arrays are sorted in the order of the first array
For example Array_multisort ($a, $b), $a, $b is two arrays, and if after sorting, the 3rd element of the $a array is ranked first, then $b's third element, regardless of his size in $b, will be ranked first. Look at the results of the program running below:
<? php $a =array (100,80,50,10,0); $b = array ("C", "F", "Q", "E", "Z" array_multisort ( $a , $b var_dump ( $a var_dump ( $b ?
Operation Result:
Array (5) {[0]=> int (0) [1]=> Int (ten) [2]=> int ([3]=> int) [4]=> int (100)}
Array (5) {[0]=> string (1) "Z" [1]=> string (1) "E" [2]=> string (1) "Q" [3]=> string (1) "F" [4]=> string (1) "C"}
It is clear that the z of the fifth element of array B is ranked first!
In fact, Array_multisort () sort the first array by the size of the key values, and then the other arrays are adjusted according to the adjustment strategy of the first array--the third element is placed first, the second element is placed in the second position ... In fact, this multi-dimensional array sorting algorithm is the most basic embodiment!
However, it is important to note that the number of elements in the two array must be the same, or a warning message will appear:
Warning:array_multisort () [Function.array-multisort]: Array sizes is inconsistent in ...
Well, I hope you can use the above, we still say the main bar: Array_multisort () to the multi-dimensional array to sort, this function will be very useful in the future to do the project!
<?PHP//Let's construct a multidimensional array$a=Array(100,2,4,7,7);$b=Array(' AB ', ' AC ', ' ad ', ' AG ', ' ap ')); $ab=Array($a,$b);//Start SortingArray_multisort($ab[0],sort_numeric,sort_desc,$ab[1],sort_string,SORT_ASC);Print_r($ab);?>
Note: First we use Sort_numeric to declare the $ab[0] by the number type, with the Sort_desc
The declaration order is reversed (from large to small), and then we sort the $ab[1] by string type, in ascending order (order)
The result of the final array $ab is the combination of the two, first press $ab[0] in reverse order, if the $ab[0] in the same size of the values are in accordance with $AB[1], the output is as follows:
Array (
[0] = = Array ([0] = [1] = 7 [2] = 7 [3] = 4 [4] = 2)
[1] = = Array ([0] = AB [1] = + AG [2] = AP [3] = = AD [4] = AC)
)
Is it much like using order by in a database? Actually, it's almost there!
<?PHP$array[] =Array("Age" =>20, "name" = "Li");$array[] =Array("Age" =>21, "name" = "AI");$array[] =Array("Age" =>20, "name" = "CI");$array[] =Array("Age" =>22, "name" = "Di"); foreach($array as $key=$value){ $age[$key] =$value[' Age ']; $name[$key] =$value[' Name '];} Array_multisort($age, Sort_numeric,sort_desc,$name, SORT_STRING,SORT_ASC,$array);Print_r($array);?>
The $array[] array of this example is constructed according to the records that are read in the database, and we now rank them in the order of age from large to small, in the order of their names, if they are the same age. This sort is what we will use in the future,
Because Array_multisort () needs a sort parameter that must be a column, we use foreach to read the age and name of the array, and then what?
Just like the example above, the last parameter $array must be seen, yes, it is necessary to declare which array to sort, because our front two parameters in the form already and need to sort the PHP array has no relationship, although they are actually the data in the $array-we The columns extracted in the $array--sort of course are required columns, have not yet seen using row data to sort it!
The output is as follows--as we thought:
Array (
[0] = = Array ([age] = [name] = di)
[1] = = Array ([age] = [name] + ai)
[2] = = Array ([age] = [name] + ci)
[3] = = Array ([age] = [name] = + li)
)
PHP array sort function array_multisort () function details (ii)