Detailed explanation of php array_multisort() function_PHP tutorial

WBOY
Release: 2016-07-20 11:02:51
Original
824 people have browsed it

php tutorial array_multisort() function detailed explanation

Function bool array_multisort ( array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $...]]] )

Parameter description: The function sorts multiple arrays or multi-dimensional arrays

The first parameter is an array, and each subsequent parameter may be an array or the following sort order flag

SORT_ASC - Default, sort in ascending order

SORT_DESC - Sort in descending order

You can then specify the sorting type

SORT_REGULAR - Default. Arrange each item in regular order.

SORT_NUMERIC - Sort each item in numerical order.

SORT_STRING - Sort each item in alphabetical order.

Example code

$arr1 = array('10', 11, 100, 100, 'a');

$arr2 = array(1, 2, 3, '2', 5);

array_multisort($arr1, $arr2);

The result is:

$arr1
Array ( [0] => 10 [1] => a [2] => 11 [3] => 100 [4] => 100 )
# '10' is converted to the integer 10 when compared with 11, 100, 100, which is smaller than the other three numbers
# '10' is used as a string when compared with 'a'. Its first character '1' has an ascii code value of 49 which is less than 'a' (the ascii value is 97), so '10' is the smallest element
# When 'a' is compared with other three numbers, it is converted to an integer 0, which is smaller than the other three numbers

$arr2
Array ( [0] => 1 [1] => 5 [2] => 2 [3] => 2 [4] => 3 )
# $arr2 element 1 corresponds to $arr1 element '10' position, so it is ranked at [0] position
# $arr1[2] => 100, $arr1[3] => 100 correspond to $arr2 elements 3 and '2' respectively. 3 is greater than '2', so $arr1[2] corresponding to 2 => 100. The sorted subscript is

3, $arr1[3] corresponding to 3 => The index of 100 sorting is 4

Summary

1. The number of array elements participating in sorting remains the same
2. The corresponding positions of the sorted array elements are as follows: ‘10’ => 1, 11 => 2
3. The following array is sorted based on the order of the previous array
4. If the previous array encounters equal elements, compare the following array

Grammar

array_multisort(array1,sorting order,sorting type,array2,array3...)
Copy after login
参数 描述
array1 必需。规定输入的数组。
sorting order 可选。规定排列顺序。可能的值是 SORT_ASC 和 SORT_DESC。
sorting type 可选。规定排序类型。可能的值是SORT_REGULAR、SORT_NUMERIC和SORT_STRING。
array2 可选。规定输入的数组。
array3 可选。规定输入的数组。

Tips and Notes

Note: String key names will be preserved, but numeric keys will be re-indexed, starting at 0 and increasing by 1.

Note: You can set the sort order and sort type after each array. If not set, each array parameter will use the default


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445334.htmlTechArticlephp tutorial array_multisort() function detailed explanation function bool array_multisort (array $arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $...]]] ) Parameter description: Function to many...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!