Home >Backend Development >PHP Tutorial >How to use the php array_multisort function to perform complex sorting of database results_PHP tutorial
First of all, let’s talk about the requirements: there are 4 fields in the database, namely id, volume, edition, and name. It is required to sort the query results from large to small according to volume+edition.
The following is the array_multisort function
array_multisort() 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.
Sort order flags:
SORT_ASC – Sort in ascending order
SORT_DESC – Sort in descending order
Sort type flags:
SORT_REGULAR - compare items according to the usual method
SORT_NUMERIC - compare items according to numeric values
SORT_STRING - compare items according to strings Two similar sorting flags cannot be specified after comparing
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 were used.
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.
The parameter structure of this function is somewhat unusual, but very flexible. The first parameter must be an array. Each of the following arguments can be an array or a sort flag listed below.
So we now have such a set of data