Home  >  Article  >  Backend Development  >  How to use the php array_multisort function to perform complex sorting of database results_PHP tutorial

How to use the php array_multisort function to perform complex sorting of database results_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:07:30907browse

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

Copy the code The code is as follows:

/ / This is a set of results queried from the database
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume ' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$ data[] = array('volume' => 67, 'edition' => 7);
//We need to make an array of volume+edition first
foreach($data as $val ){
$arr[] = $val['volume'] + $val['edition'];
}
// Sort $arr in descending order
// Use $data as The last parameter, sort by common key
array_multisort($arr, SORT_DESC, $data);

This achieves the function we need

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327546.htmlTechArticleFirst let’s talk about the requirements: there are 4 fields in the database, namely id, volume, edition, name. The query is required The results are sorted from large to small according to volume+edition. Let’s take a look at the array_multisort function...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn