Database query results sometimes can not be used directly, such as MySQL with in statements out of the results, so need to sort the results in some way.
Example 4. To sort the results of a database
In this example, each cell in the data array represents a row in a table. This is a data 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
The data is all stored in an array named data. This is usually done by looping the results from the database, such as MYSQL_FETCH_ASSOC ().
$data [] = Array (' volume ' = +, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 1);
$data [] = Array (' volume ' = =, ' edition ' = 6);
$data [] = Array (' volume ' = = 98, ' edition ' = 2);
$data [] = Array (' volume ' = +, ' edition ' = 6);
$data [] = Array (' volume ' = +, ' edition ' = 7);
?>
In this example, the volume is sorted in descending order, and the edition in ascending order.
Now that you have an array with rows, but Array_multisort () requires an array of columns, so use the following code to get the columns and sort them.
Get a list of columns
foreach ($data as $key = = $row) {
$volume [$key] = $row [' volume '];
$edition [$key] = $row [' Edition '];
}
Sort data in descending order according to volume, in ascending order of edition
The $data as the last parameter, sorted by the Universal key
Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);
?>
The data set is now sorted and the results are as follows:
Volume | Edition
-------+--------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7
http://www.bkjia.com/PHPjc/446955.html www.bkjia.com true http://www.bkjia.com/PHPjc/446955.html techarticle database query Results sometimes can not be used directly, such as MySQL with in statements out of the results, so need to sort the results in some way. Example 4. Row the database results ...