PHP uses Array_multisort to sort multiple arrays or multidimensional arrays, and the PHP multidimensional array is sorted
PHP Array_multisort can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional.
The association (string) key name remains the same, but the numeric key name is re-indexed.
The input array is treated as a column of a table and sorted in rows-this is similar to the function of the SQL ORDER by clause. The first array is the primary array to sort. The rows (values) in the array are compared to the same words, sorted by the size of the corresponding values in the next input array, and so on. This is the key to understanding the use of this function.
The first argument must be an array. Each of the following parameters can be an array or a sort flag listed below.
Sort order Flags:
Sort_asc-Sort by ascending order
Sort_desc-Sort by descending order
Sort Type flag:
Sort_regular-Compare items by usual method
Sort_numeric-Compare items by value
Sort_string-Compare items by string
You cannot specify two of the same sort flags after each array. The sort flags specified after each array are valid only for the array-before this is the default value Sort_asc and Sort_regular.
Take a look at two practical examples:
1. Sort multiple arrays at once:
Copy the Code code as follows:
$num 1 = Array (3, 5, 4, 3);
$num 2 = Array (27, 50, 44, 78);
Array_multisort ($num 1, SORT_ASC, $num 2, Sort_desc);
Print_r ($num 1);
Print_r ($num 2);
Result:array ([0] = 3 [1] = 3 [2] = 4 [3] = 5) Array ([0] = [1] = [2] = + [3] =&G T 50)
2. Sort multidimensional arrays (with two-bit arrays as examples):
Copy the Code code as follows:
$arr = Array (
' 0 ' = = Array (
' Num1 ' = 3,
' Num2 ' = 27
),
' 1 ' = = Array (
' Num1 ' = 5,
' Num2 ' = 50
),
' 2 ' = = Array (
' Num1 ' = 4,
' Num2 ' = 44
),
' 3 ' = = Array (
' Num1 ' = 3,
' Num2 ' = 78
)
);
foreach ($arr as $key = = $row) {
$num 1[$key] = $row [' NUM1 '];
$num 2[$key] = $row [' num2 '];
}
Array_multisort ($num 1, SORT_ASC, $num 2, Sort_desc, $arr);
Print_r ($arr);
Result:array ([0]=>array ([num1]=>3] [num2]=>78] [1]=>array [num1]=>3] [num2]=>27] [2]=>Array ([ num1]=>4 [num2]=>44] [3]=>array ([Num1]=>5 [num2]=>50)]
Summarize:
The point here is that the key to be sorted is stored in a one-dimensional array, then you can use the Array_multisort () function to sort the array by key, of course, the sort you can not apply array_multisort () This function, This effect can be achieved only with a foreach traversal, but since PHP developers have provided us with a better approach, we can eliminate unnecessary hassles.
http://www.bkjia.com/PHPjc/928219.html www.bkjia.com true http://www.bkjia.com/PHPjc/928219.html techarticle PHP uses Array_multisort to sort multiple arrays or multidimensional arrays, and PHP array_multisort can be used to sort multiple arrays at once, or according to a dimension or ...