How to Use the php array_multisort function to sort complex database results

Source: Internet
Author: User

First, let's talk about the requirement: the database has four fields: id, volume, edition, and name. The query results must be sorted in descending order of volume + edition.
The following describes the array_multisort function.
Array_multisort () can be used to sort multiple arrays at a time, or to sort multi-dimensional arrays based on one or more dimensions.

The Association (string) key name remains unchanged, but the number key name is re-indexed.

Sort order mark:
SORT_ASC-sort by Ascending Order
SORT_DESC-sort by descent

Sorting type flag:
SORT_REGULAR-compare projects by common methods
SORT_NUMERIC-compare projects by numerical values
SORT_STRING-compare items by string

You cannot specify two sorting flags of the same type after each array. The sorting flag specified after each array is only valid for this array-the default values are SORT_ASC and SORT_REGULAR.

The input array is treated as a table column and ordered BY rows-similar to the SQL ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array are the same, they are sorted by the corresponding values in the next input array.

The parameter structure of this function is somewhat unusual, but flexible. The first parameter must be an array. Each of the following parameters can be an array or a sorting marker listed below.

Now we have such a set of data.

Copy codeThe Code is as follows: // This is the result of a group of queries from the database.
$ 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 );
// We need to first make an array of volume + edition
Foreach ($ data as $ val ){
$ Arr [] = $ val ['Volume '] + $ val ['version'];
}
// Sort $ arr in descending order
// Use $ data as the last parameter and sort it by a common key
Array_multisort ($ arr, SORT_DESC, $ data );

In this way, the functions we need are implemented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.