PHP uses Array_multisort to sort multiple arrays or multi-dimensional arrays, PHP multidimensional array sorting _php tutorial

Source: Internet
Author: User

PHP uses Array_multisort to sort multiple arrays or multidimensional arrays, and the PHP multidimensional array is sorted


PHP Array_multisort can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional.

The association (string) key name remains the same, but the numeric key name is re-indexed.
The input array is treated as a column of a table and sorted in rows-this is similar to the function of the SQL ORDER by clause. The first array is the primary array to sort. The rows (values) in the array are compared to the same words, sorted by the size of the corresponding values in the next input array, and so on. This is the key to understanding the use of this function.

The first argument must be an array. Each of the following parameters can be an array or a sort flag listed below.

Sort order Flags:
Sort_asc-Sort by ascending order
Sort_desc-Sort by descending order

Sort Type flag:
Sort_regular-Compare items by usual method
Sort_numeric-Compare items by value
Sort_string-Compare items by string

You cannot specify two of the same sort flags after each array. The sort flags specified after each array are valid only for the array-before this is the default value Sort_asc and Sort_regular.

Take a look at two practical examples:

1. Sort multiple arrays at once:

Copy the Code code as follows:
$num 1 = Array (3, 5, 4, 3);
$num 2 = Array (27, 50, 44, 78);
Array_multisort ($num 1, SORT_ASC, $num 2, Sort_desc);

Print_r ($num 1);
Print_r ($num 2);
Result:array ([0] = 3 [1] = 3 [2] = 4 [3] = 5) Array ([0] = [1] = [2] = + [3] =&G T 50)

2. Sort multidimensional arrays (with two-bit arrays as examples):

Copy the Code code 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) {
$num 1[$key] = $row [' NUM1 '];
$num 2[$key] = $row [' num2 '];
}
Array_multisort ($num 1, SORT_ASC, $num 2, 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)]

Summarize:

The point here is that the key to be sorted is stored in a one-dimensional array, then you can use the Array_multisort () function to sort the array by key, of course, the sort you can not apply array_multisort () This function, This effect can be achieved only with a foreach traversal, but since PHP developers have provided us with a better approach, we can eliminate unnecessary hassles.

http://www.bkjia.com/PHPjc/928219.html www.bkjia.com true http://www.bkjia.com/PHPjc/928219.html techarticle PHP uses Array_multisort to sort multiple arrays or multidimensional arrays, and PHP array_multisort can be used to sort multiple arrays at once, or according to a dimension or ...

  • Related Article

    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.