First, let's talk about requirements: There are 4 fields in the database, respectively, Id,volume,edition,name. The query results are required to be sorted from large to small according to Volume+edition.
Here's a look at the Array_multisort function
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 unchanged, but the numeric key name is indexed.
Sort order Flags:
sort_asc– Sorted by ascending order
sort_desc– Sorted in descending order
Sort Type Flags:
sort_regular– items are compared in the usual way
sort_numeric– the project by numerical comparison
sort_string– items by string comparison
You cannot specify two similar sort flags after each array. The sort flags specified after each array are valid only for the array-the default value SORT_ASC and Sort_regular before that.
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 the same, sorted by the size of the corresponding values in the next input array, and so on.
The parameter structure of this function is somewhat unusual, but very flexible. The first argument must be an array. Each of the next parameters can be an array or a sort flag listed below.
So now we have this set of data
Copy Code code as follows:
This is the result of a set of queries from the database
$data [] = Array (' volume ' =>, ' Edition ' => 2);
$data [] = Array (' volume ' =>, ' edition ' => 1);
$data [] = Array (' volume ' =>, ' Edition ' => 6);
$data [] = Array (' volume ' =>, ' Edition ' => 2);
$data [] = Array (' volume ' =>, ' Edition ' => 6);
$data [] = Array (' volume ' =>, ' edition ' => 7);
We need to first make an array of volume+edition to
foreach ($data as $val) {
$arr [] = $val [' volume '] + $val [' Edition '];
}
Arrange $arr according to descending order
Sort the $data as the last parameter in a common key
Array_multisort ($arr, Sort_desc, $data);
This will enable us to achieve the functions we need.