Use the array_multisort () function to sort multiple arrays or multi-dimensional arrays. If you are studying array sorting and multi-dimensional array sorting, you can see.
Function bool array_multisort (array & $ arr [, mixed $ arg = SORT_ASC [, mixed $ arg = SORT_REGULAR [, mixed $...])
Parameter description: The function sorts multiple arrays or multi-dimensional arrays.
The first parameter is an array, and each subsequent parameter may be an array or the following sort order mark.
SORT_ASC-default, in ascending order
SORT_DESC-sort in descending order
You can specify the sorting type.
SORT_REGULAR-default. Sort each item in the general order.
SORT_NUMERIC-Sort each item in numerical order.
SORT_STRING-Sort each item in alphabetical order.
Example:
Copy codeThe Code is as follows:
<? Php
$ Arr1 = array ('10', 11,100,100, 'A ');
$ Arr2 = array (1, 2, 3, '2', 5 );
Array_multisort ($ arr1, $ arr2 );
?>
Result:
$ Arr1
Array ([0] => 10 [1] => a [2] => 11 [3] => 100 [4] => 100)
# '10' is converted to an integer 10 when compared with 11,100,100, which is smaller than the other three.
# '10' is a string when 'A' is compared. Its first character '1' ascii code value is 49 less than 'A' (ascii value is 97 ), therefore, '10' is the minimum element.
# When 'A' is compared with the other three numbers, it is converted to an integer 0, which is smaller than the other three.
$ Arr2
Array ([0] => 1 [1] => 5 [2] => 2 [3] => 2 [4] => 3)
# $ Arr2 element 1 corresponds to $ arr1 element '10', so it is in the [0] position.
# $ Arr1 [2] => 100, $ arr1 [3] => 100 corresponds to $ arr2 element 3, '2' respectively '. 3 is greater than '2', so the subscript corresponding to $ arr1 [2] => 100 after sorting is 3, the subscript of $ arr1 [3] => 100 sorting is 4.
Conclusion ----------
1. The number of elements in the array to be sorted must be consistent.
2. The position of the elements in the sorting array corresponds to, for example, '10' => 1, 11 => 2.
3. The back array is sorted based on the order of the front array.
4. In case of equal elements, the array of the front edge compares the Array
The following is an example of use. The examples in the manual are not listed, so I have several examples in my work.
The most common problem encountered at work is a two-dimensional array. I want to try a three-dimensional array, but forget it.
Copy codeThe Code is as follows:
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 ---------------------