Array_multisort usage
I. first look at the simplest situation. There are two Arrays:
$ Arr1 = array (1, 9, 5 );
$ Arr2 = array (6, 2, 4 );
Array_multisort ($ arr1, $ arr2 );
Print_r ($ arr1); // The order is, 9.
Print_r ($ arr2); // The Order is 6, 4, 2.
I estimate that the values of the two arrays correspond from beginning to end: 1 corresponds to 6, 9 corresponds to 2, 5 corresponds to 4.
Let's add an array to see what will happen:
$ Arr1 = array (1, 9, 5 );
$ Arr2 = array (6, 2, 4 );
$ Arr3 = array (3, 7, 8 );
Array_multisort ($ arr1, $ arr2, $ arr3 );
View the result. 1 corresponds to 6 and 3 from start to end. This is also true for other items. This correspondence is the so-called "keep the original key-name association during sorting" in the manual ".
You can also think of each array as a column in a database table. The 1, 6, and 3 corresponding to a data row, 9, 2, and 7 are another data row...
Array_multisort will first sort by the first array (as expected). If the value of the first array (column) is the same, it will sort by the second array (column.
You can use the following program to test the function:
$ Arr1 = array (1, 9, 5, 9 );
$ Arr2 = array (6, 2, 4, 1 );
$ Arr3 = array (3, 7, 8, 0 );
Array_multisort ($ arr1, $ arr2, $ arr3 );
The result of $ arr3 is ).
II. The following describes the parameters of array_multisort.
The parameters of this function are flexible. The simplest case is that one or n arrays are used as parameters as shown above. Note that the number of items in each array must be the same; otherwise, the sorting will fail due to warning.
Array_multisort ($ arr1, $ arr2, $ arr3); by default, all arrays are sorted in ascending order. If you want to sort $ arr2 in descending order and compare it as a string, you must write it as follows:
Array_multisort ($ arr1, $ arr2, SORT_DESC, SORT_STRING, $ arr3 );
Each array can be followed by a sort order sign or a sort type sign, or both. However, each sort flag can only appear after each array.
The details are as follows:
Sort order mark:
SORT_ASC-sort by Ascending Order (default)
SORT_DESC-sort by descent
Sorting type flag:
SORT_REGULAR-compare projects by the common method (default)
SORT_NUMERIC-compare projects by numerical values
SORT_STRING-compare items by string
Actual purpose:After the data is extracted from the database, it may be an array. If you want to sort the data by that field, you can extract the field to form a separate array, then use array_multisort to sort the original data by a certain field. The following is a small example.
Copy codeThe Code is as follows:
<? Php
$ A = array ('name' => 'zhang san', 'score' => 60 ),
Array ('name' => 'Li si', 'score '=> 90 ),
Array ('name' => 'wang 2', 'score '=> 80)
);
$ Score = array ();
Foreach ($ a as $ k => $ v ){
$ Score [$ k] = $ v ['score '];
}
Array_multisort ($ score, $ );
Var_dump ($ score );
Var_dump ($ );
?>
As a result, run it on your own.