Home > Backend Development > PHP Tutorial > Detailed explanation of PHP array sorting_PHP tutorial

Detailed explanation of PHP array sorting_PHP tutorial

WBOY
Release: 2016-07-15 13:25:08
Original
868 people have browsed it

PHP array sorting is actually sorting PHP arrays. In this article, it is sorting the result set from the database query. Database query results sometimes cannot be used directly, such as the results obtained using the in statement in mysql, so the results need to be sorted in some way. At this time, you need to sort the PHP array. To sort database results, see the following example:

In this example, each cell in the data array represents a row in a table. This is a typical way for databases to store array data.

The data in the example is as follows:

volume | edition
-------+--------
67 | 2
86 | 1
85 | 6
98 | 2
86 | 6
67 | 7

The data are all stored in the array named data. This is usually the result obtained from the database through a loop, such as mysql_fetch_assoc() (in fact, you can think of this function as the same as the mysql_fetch_assoc() function. For specific differences, you can read the difference in keys in the PHP manual).

$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);

In this example, the volume will be sorted in descending order and the edition will be sorted in ascending order.

Now you have an array with rows, but array_multisort() requires an array with columns, so use the following code to get the columns and then sort them.

// 取得列的列表foreach ($data as $key => $row) {    $volume[$key]  = $row['volume'];    $edition[$key] = $row['edition'];}// 将数据根据 volume 降序排列,根据 edition 升序排列// 把 $data 作为最后一个参数,以通用键排序array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
Copy after login

The data collection is now sorted, and the results are as follows:

volume | edition
-------+--------
98 | Methods can be used, such as arsort(), asort(), ksort(), krsort(), natsort(), natcasesort(), rsort(), usort(), array_multisort() and uksort() that come with PHP’s array array. ).




  1. http://www.bkjia.com/PHPjc/446711.html
www.bkjia.com
true

http: //www.bkjia.com/PHPjc/446711.htmlTechArticlePHP array sorting is actually sorting PHP arrays. In this article, it is the result set of the database query. to sort. Database query results sometimes cannot be used directly, than...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template