PHP array sort function array_multisort () function (i)

Source: Internet
Author: User

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:

$num 1 Array (3, 5, 4, 3); $num 2 Array (+,--); Array_multisort ($num 1$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] = 44 [3] = +)

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

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

PHP Array sort function array_multisort () function (i)

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.