The array in the parameter is treated as a table column and sorted by rows-similar to the SQL order by clause function. The first array is the main array to be sorted. If the rows (values) in the array are the same, they are sorted according to the corresponding values in the next input array.
<? Php Tutorial
$ A1 = array ("dog", "cat ");
$ A2 = array ("fido", "missy ");
Array_multisort ($ a1, $ a2 );
Print_r ($ a1 );
Print_r ($ a2 );
?> Output:
Array ([0] => cat [1] => dog)
Array ([0] => missy [1] => fido)
Example 6. Ranking
<? Php
$ Grade = array ("score" => array (70, 95, 70.0, 60, "70 "),
"Name" => array ("zhang san", "li si", "wang wu ",
"Zhao liu", "liu qi "));
Array_multisort ($ grade ["score"], sort_numeric, sort_desc,
// Take the score as a value, sorted from high to low
$ Grade ["name"], sort_string, sort_asc );
// Use the name as a string in ascending order
Var_dump ($ grade );
?>
The above example will output:
Array (2 ){
["Score"] =>
Array (5 ){
[0] =>
Int (95)
[1] =>
String (2) "70"
[2] =>
Float (70)
[3] =>
Int (70)
[4] =>
Int (60)
}
["Name"] =>
Array (5 ){
[0] =>
String (5) "li si"
[1] =>
String (6) "liu qi"
[2] =>
String (7) "wang wu"
[3] =>
String (9) "zhang san"
[4] =>
String (8) "zhao liu"
}
}
Multi-dimensional array sorting:
<? Php
$ Arr = array (1, 2, 3, 4), array ("a", "B", "B", "d "));
Array_multisort ($ arr [0], sort_desc, $ arr [1], sort_asc );
Print_r ($ arr );
?>
Run the sample output:
Array
(
[0] => array
(
[0] => 4
[1] => 2
[2] => 2
[3] => 1
)
[1] => array
(
[0] => d
[1] => B
[2] => c
[3] =>
)
)
The array_multisort () function sorts multiple arrays or multi-dimensional arrays.