PHP uses array_multisort to sort multiple arrays or multi-dimensional arrays. php multi-dimensional array sorting _ PHP Tutorial

Source: Internet
Author: User
PHP uses array_multisort to sort multiple arrays or multi-dimensional arrays, and php multi-dimensional arrays. PHP uses array_multisort to sort multiple arrays or multi-dimensional arrays. in php, array_multisort can be used to sort multiple arrays at a time, you can also use array_multisort to sort multiple arrays or multi-dimensional arrays based on a certain dimension or PHP, and use php to sort multi-dimensional arrays.

In PHP, 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.
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. -- This sentence is the key to understanding the usage of this function.

The first parameter must be an array. Each of the following parameters can be an array or a sorting marker listed below.

Sort order mark:
■ SORT_ASC-sort by ascending order
■ SORT_DESC-sort by descent

Sorting type flag:
■ SORT_REGULAR-compare projects according to 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.

Let's take a look at two practical examples:

1. sort multiple arrays at a time:

The code is as follows:


$ Num1 = array (3, 5, 4, 3 );
$ Num2 = array (27, 50, 44, 78 );
Array_multisort ($ num1, SORT_ASC, $ num2, SORT_DESC );

Print_r ($ num1 );
Print_r ($ num2 );
// Result: Array ([0] => 3 [1] => 3 [2] => 4 [3] => 5) array ([0] => 78 [1] => 27 [2] => 44 [3] => 50)

2. sort multidimensional arrays (taking the two-dimensional array as an example:

The code is as follows:


$ Arr = array (
'0' => array (
'Num1' => 3,
'Num2' => 27
),
'1' => array (
'Num1' => 5,
'Num2' => 50
),
'2' => array (
'Num1' => 4,
'Num2' => 44
),
'3' => array (
'Num1' => 3,
'Num2' => 78
)
);
Foreach ($ arr as $ key => $ row ){
$ Num1 [$ key] = $ row ['num1'];
$ Num2 [$ key] = $ row ['num2'];
}
Array_multisort ($ num1, SORT_ASC, $ num2, SORT_DESC, $ arr );
Print_r ($ arr );
// Result: Array ([0] => Array ([num1] => 3 [num2] => 78) [1] => Array ([num1] => 3 [num2] => 27) [2] => Array ([num1] => 4 [num2] => 44) [3] => Array ([num1] => 5 [num2] => 50 ))


Summary:

The key here is to save the key to be sorted to a one-dimensional array, and then you can use the array_multisort () function to sort the array by key. of course, the sorting here is not applicable to the array_multisort () function, which can be achieved only through foreach traversal. However, since php developers provide us with a better solution, we can save unnecessary trouble.

In structured PHP, array_multisort can be used to sort multiple arrays at a time, or according to a certain dimension or...

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.