Home >Backend Development >PHP Tutorial >PHP uses array_multisort to sort multiple arrays or multidimensional arrays, php multidimensional array sorting_PHP tutorial
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:
2. Sort multi-dimensional arrays (taking two-digit arrays as an example):
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.