First, let's talk about the requirement: the database has four fields: id, volume, edition, and name. The query results must be sorted in descending order of volume + edition.
The following describes the array_multisort function.
Array_multisort () can be used to sort multiple arrays at a time, or to sort multi-dimensional arrays based on one or more dimensions.
The Association (string) key name remains unchanged, but the number key name is re-indexed.
Sort order mark:
SORT_ASC-sort by Ascending Order
SORT_DESC-sort by descent
Sorting type flag:
SORT_REGULAR-compare projects by common methods
SORT_NUMERIC-compare projects by numerical values
SORT_STRING-compare items by string
You cannot specify two sorting flags of the same type after each array. The sorting flag specified after each array is only valid for this array-the default values are SORT_ASC and SORT_REGULAR.
The input array is treated as a table column and ordered BY rows-similar to the SQL ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array are the same, they are sorted by the corresponding values in the next input array.
The parameter structure of this function is somewhat unusual, but flexible. The first parameter must be an array. Each of the following parameters can be an array or a sorting marker listed below.
Now we have such a set of data.
Copy codeThe Code is as follows: // This is the result of a group of queries from the database.
$ Data [] = array ('Volume '=> 67, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 1 );
$ Data [] = array ('Volume '=> 85, 'version' => 6 );
$ Data [] = array ('Volume '=> 98, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 6 );
$ Data [] = array ('Volume '=> 67, 'version' => 7 );
// We need to first make an array of volume + edition
Foreach ($ data as $ val ){
$ Arr [] = $ val ['Volume '] + $ val ['version'];
}
// Sort $ arr in descending order
// Use $ data as the last parameter and sort it by a common key
Array_multisort ($ arr, SORT_DESC, $ data );
In this way, the functions we need are implemented.