Bool array_multisort (array ar1 [, mixed arg [, mixed... [, array...])
If the call succeeds, TRUE is returned. If the call fails, FALSE is returned.
Array_multisort () can be used to sort multiple arrays at a time, or to sort multi-dimensional arrays based on one or more dimensions.
The association (string) key name remains unchanged, but the number key name is re-indexed.
Example 1. Sort multidimensional arrays
| The code is as follows: |
Copy code |
|
<? Php
$ Ar = array (
Array ("10", 11,100,100, ""),
Array (1, 2, "2", 3, 1)
);
Array_multisort ($ ar [0], SORT_ASC, SORT_STRING,
$ Ar [1], SORT_NUMERIC, SORT_DESC );
Var_dump ($ ar );
?> |
In this example, after sorting, the first array will transform to "10", 100,100, 11, "a" (it was sorted as strings in ascending order ). the second will contain in 1, 3, "2", 2, 1 (sorted as numbers, in descending order ).
| The code is as follows: |
Copy code |
|
Array (2 ){
[0] => array (5 ){
[0] => string (2) "10"
[1] => int (100)
[2] => int (100)
[3] => int (11)
[4] => string (1) ""
}
[1] => array (5 ){
[0] => int (1)
[1] => int (3)
[2] => string (1) "2"
[3] => int (2)
[4] => int (1)
}
} |
In this example, after sorting, the first array will contain 10,100,100, "a" (as the string ascending order), and the second array will contain 1, 3, "2 ", 1 (sort the values in descending order ).
Example 2. Sorting multi-dimen1_array
| The code is as follows: |
Copy code |
|
<? Php
$ Ar = array (
Array ("10", 11,100,100, ""),
Array (1, 2, "2", 3, 1)
);
Array_multisort ($ ar [0], SORT_ASC, SORT_STRING,
$ Ar [1], SORT_NUMERIC, SORT_DESC );
Var_dump ($ ar );
?> |
In this example, after sorting, the first array will become "10", 100,100, 11, "a" (sorted in ascending order as strings ). The second array will contain 1, 3, "2", 2, 1 (arranged in descending order as numbers ).
| The code is as follows: |
Copy code |
Array (2 ){
[0] => array (5 ){
[0] => string (2) "10"
[1] => int (100)
[2] => int (100)
[3] => int (11)
[4] => string (1) ""
}
[1] => array (5 ){
[0] => int (1)
[1] => int (3)
[2] => string (1) "2"
[3] => int (2)
[4] => int (1)
}
}
|
Instance 3 provides a comprehensive implementation of common instances in applications.
| The code is as follows: |
Copy code |
Header ('content-Type: text/html; charset = utf-8 ');
Echo '<pre> ';
// Original array format
$ Array = array (
'Key1' => array (
'Item1' => '65 ',
'Item2' => '35 ',
'Item3' => '84 ',
),
'Key2' => array (
'Item1' => '24 ',
),
'Key3' => array (
'Item1' => '38 ',
'Item3' => '45 ',
),
);
// Key to be sorted
// Sort by item1 in the array
// You can also change to item2
$ Sort = 'item1 ';
Foreach ($ array as $ k => $ v)
{
$ NewArr [$ k] = $ v [$ sort];
}
// If this function is correctly executed, it will directly change the order of the original array key values
// If the execution fails, bool (false) is returned)
Array_multisort ($ newArr, SORT_DESC, $ array );
Var_dump ($ array );
// --------------------- The print effect of the sorted array starts --------------------
Array (3 ){
["Key1"] =>
Array (3 ){
["Item1"] =>
String (2) "65"
["Item2"] =>
String (2) "35"
["Item3"] =>
String (2) "84"
}
["Key3"] =>
Array (2 ){
["Item1"] =>
String (2) "38"
["Item3"] =>
String (2) "45"
}
["Key2"] =>
Array (1 ){
["Item1"] =>
String (2) "24"
}
}
// --------------------- The print effect of the sorted array is over --------------------- |
For details about the array_multisort () function, refer to the http://www.111cn.net/phper/php-function/39192.htm