Home >Backend Development >PHP Tutorial >PHP uses array_multisort to sort multiple arrays or multidimensional arrays, php multidimensional array sorting_PHP tutorial

PHP uses array_multisort to sort multiple arrays or multidimensional arrays, php multidimensional array sorting_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:11:431019browse

PHP uses array_multisort to sort multiple arrays or multi-dimensional arrays, php multi-dimensional array sorting

array_multisort in PHP can be used to sort multiple arrays at one time, or to sort multi-dimensional arrays according to a certain dimension or multiple dimensions.

Associative (string) key names remain unchanged, but numeric key names will be re-indexed.
The input array is treated as a table column and sorted by row - this is similar to the functionality of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array are compared to be the same, they are sorted according to the size of the corresponding value in the next input array, and so on. ——This sentence is the key to understanding the usage of this function.

The first parameter must be an array. Each of the following arguments can be an array or a sort flag listed below.

Sort order flag:
■SORT_ASC - Sort in ascending order
■SORT_DESC - Sort in descending order

Sort type flag:
■SORT_REGULAR - Compare items in the usual way
■SORT_NUMERIC - Compare items numerically
■SORT_STRING - Compare items by string

You cannot specify two sorting flags of the same type after each array. The sort flags specified after each array are valid only for that array - before that, the default values ​​SORT_ASC and SORT_REGULAR.

Look at two practical examples:

1. Sort multiple arrays at one time:

Copy code The code is as follows:

$num1 = array(3, 5, 4, 3);
$num2 = array(27, 50, 44, 78);
array_multisort($num1, SORT_ASC, $num2, SORT_DESC);

print_r($num1);
print_r($num2);
//result: Array ( [0] => 3 [1] => 3 [2] => 4 [3] => 5 ) Array ( [0] => 78 [1] => 27 [2] => 44 [3] => 50 )

2. Sort multi-dimensional arrays (taking two-digit arrays as an example):

Copy code The code is 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 ){
$num1[$key] = $row ['num1'];
$num2[$key] = $row ['num2'];
}
array_multisort($num1, SORT_ASC, $num2, 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))

Summary:

The key point here is to first store the keys to be sorted into a one-dimensional array, and then you can use the array_multisort() function to sort the array according to the keys. Of course, you don’t have to apply the sorting here. The array_multisort() function can also achieve this effect just through foreach traversal, but since PHP developers have provided us with a better way, we can save unnecessary trouble.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/928219.htmlTechArticlePHP uses array_multisort to sort multiple arrays or multi-dimensional arrays. php multi-dimensional array sorting array_multisort in PHP can be used once Sort multiple arrays, either by a certain dimension or...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn