A function that uses the Array_multisort () function to sort multiple arrays or multidimensional arrays, and a friend who is studying array sorting and multidimensional array ordering can look at it.
function bool Array_multisort (array & $arr [, Mixed $arg = Sort_asc [, Mixed $arg = Sort_regular [, mixed $ ...]])
Parameter description: function to sort multiple arrays or multidimensional arrays
The first argument is an array, and each subsequent argument may be an array, or it may be the following sort order flag
SORT_ASC-Default, in ascending order
Sort_desc-In descending order
You can then specify the sort type
Sort_regular-Default. Arranges each item in a regular order.
Sort_numeric-Arranges each item in numerical order.
Sort_string-Arranges each entry in alphabetical order.
Cases:
Copy Code code as follows:
<?php
$arr 1 = Array (' Ten ', one, one,,, ' a ');
$arr 2 = Array (1, 2, 3, ' 2 ', 5);
Array_multisort ($arr 1, $arr 2);
?>
The results are:
$arr 1
Array ([0] => [1] => a [2] => [3] => [4] => 100)
# ' 10 ' is converted to an integer 10 when compared with 11, 100, 100, and less than the other three numbers
# ' 10 ' is a ' a ' comparison as a string whose first character ' 1 ' has an ASCII code value of 49 less than ' a ' (ASCII value 97), so ' 10 ' is the smallest element
# ' A ' is converted to an integer 0, less than three other numbers when the other three digits are compared
$arr 2
Array ([0] => 1 [1] => 5 [2] => 2 [3] => 2 [4] => 3)
# $arr 2 element 1 corresponds to the $ARR1 element ' 10 ' position, so it's positioned at [0]
# $arr 1[2] =>, $arr 1[3] => 100 correspond to $ARR2 elements 3, ' 2 ' respectively. 3 is greater than ' 2 ', so with 2 corresponding $arr1[2] => 100 sorted subscript 3, with 3 corresponding $arr1[3] => 100 sorted subscript 4
Summary----------
1. The number of array elements involved in sorting remains consistent
2. Sort array element position corresponding to, ' Ten ' => 1, one => 2
3. The array is sorted based on the order of the preceding array
4. The preceding array, when equal elements are compared, compares the rear array
Below is the example used, the manual example is not listed, on my own work in a few.
The most likely
work is a two-dimensional array, and you want to try a three-dimensional array, but think about it or forget it.
Copy Code code as follows:
header (' content-type:text/html; Charset=utf-8 ');
Echo ' <pre> ';
//original array format
$array = Array (
' key1 ' => array (
' item1 ' => '),
' item2 ' => ',
' it Em3 ' => ',
),
' Key2 ' => array (
' item1 ' => ',
),
' Key3 ' => array (
' it Em1 ' => ',
' item3 ' => ',
',
);
///To sort the keys
//According to the item1 in the array
//You can also change to item2
$sort = ' item1 ';
foreach ($array as $k => $v)
{
$newArr [$k] = $v [$sort];
}
///This function will directly change the order of the original array key values
//If execution fails, then he will return bool (false)
Array_multisort ($NEWARR, Sort_desc, $array) ;
Var_dump ($array); The
//---------------------the array print effect after sorting begins--------------------
Array (3) {
["Key1"]=>
Array (3) {
] "Item1"]=>
String (2) ""
["item2"]=>
String (2) ""
["Item3"]=>
String (2) "84" br>}
[Key3]]=>
Array (2) {
["item1"]=>
String (2) ""
["Item3"]=>
String (2) "" "
}
[" Key2 "]=>
Array (1) {
[" item1 "]=>
String (2)"
}
}
//---------------------sorted array The print effect ends---------------------