Home > Backend Development > PHP Tutorial > PHP array sorting of special statement query results_PHP tutorial

PHP array sorting of special statement query results_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-15 13:22:41
Original
1074 people have browsed it

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.

Example 4. Sorting database results

In this example, each cell in the data array represents a row in a table. This is a typical collection of data recorded in a database.

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 obtained from the database through a loop, such as mysql_fetch_assoc().

$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, Volume is sorted in descending order, and edition is 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.

// Get the list of columns

foreach ($data as $key => $row) {

$volume[$key] = $row['volume '];

$edition[$key] = $row['edition'];

}

// Sort the data in descending order according to volume and in ascending order according to edition

// Take $data as the last parameter, sort by common key

array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);

?> ;

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

volume | edition

-----+--- -----

98 | 2

86 | 1

86 | 6

85 | 6

67 | 2

67 | 7


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446955.htmlTechArticleSometimes the database query results cannot be used directly, such as the results obtained by using the in statement in mysql, so the results need to be processed Sort of some sort. Example 4. Sorting database results...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template