Php sorts the query results of special statements in arrays. php sorts the query results of special statements in arrays. database query results cannot be directly used, for example, mysql and other results obtained using in statements, therefore, you need to sort the results in some way. Example 4. sort database results each unit in the data array in this example represents a table "> <LINKhref =" http://www.ph
Sometimes the database query results cannot be directly used. for example, mysql and other results returned by an in statement must be sorted in some way.
Example 4. sort database results
In this example, each unit in the data array represents a row in a table. This is a collection of typical database records.
The data in the example is as follows:
Volume | edition
------- + --------
67 | 2
86 | 1
85 | 6
98 | 2
86 | 6
67 | 7
All data is stored in an array named data. This is usually the result obtained from the database through a loop, such as mysql_fetch_assoc ().
$ 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 );
?>
Http://sucai.knowsky.com/
In this example, we will sort volume in descending order and edition in ascending order.
Now we have an array containing rows, but array_multisort () needs an array containing columns. Therefore, we use the following code to obtain and sort columns.
// Retrieve the column list
Foreach ($ data as $ key => $ row ){
$ Volume [$ key] = $ row ['Volume '];
$ Edition [$ key] = $ row ['version'];
}
// Sort data by volume in descending order and edition in ascending order
// Use $ data as the last parameter and sort it by a common key
Array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data );
?>
The data set is sorted as follows:
Volume | edition
------- + --------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7