Php array_multisort () function details

Source: Internet
Author: User

 

Php tutorial array_multisort () function details

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 mark.

SORT_ASC-default, in ascending order

SORT_DESC-sort in descending order

You can specify the sorting type.

SORT_REGULAR-default. Sort each item in the general order.

SORT_NUMERIC-Sort each item in numerical order.

SORT_STRING-Sort each item in alphabetical order.

Instance code

$ Arr1 = array ('10', 11,100,100, 'A ');

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

Array_multisort ($ arr1, $ arr2 );

Result:

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

$ Arr2
Array ([0] => 1 [1] => 5 [2] => 2 [3] => 2 [4] => 3)
# $ Arr2 element 1 corresponds to $ arr1 element '10', so it is in the [0] position.
# $ Arr1 [2] => 100, $ arr1 [3] => 100 corresponds to $ arr2 element 3, '2' respectively '. 3 is greater than '2'. Therefore, the subscript corresponding to $ arr1 [2] => 100 after sorting is

3. The subscript of $ arr1 [3] => 100 sorting is 4.

Summary

1. The number of elements in the array to be sorted must be consistent.
2. The position of the elements in the sorting array corresponds to, for example, '10' => 1, 11 => 2.
3. The back array is sorted based on the order of the front array.
4. In case of equal elements, the array of the front edge compares the Array

Syntax
array_multisort(array1,sorting order,sorting type,array2,array3...)
Parameters Description
Array1 Required. Specifies the input array.
Sorting order Optional. Specify the order. Possible values are SORT_ASC and SORT_DESC.
Sorting type Optional. Specifies the sorting type. Possible values are SORT_REGULAR, SORT_NUMERIC, and SORT_STRING.
Array2 Optional. Specifies the input array.
Array3 Optional. Specifies the input array.
Tips and comments

Note: The string key will be retained, but the number key will be re-indexed, starting from 0 and increasing at 1.

Note: You can set the sorting order and type behind each array. If no value is set, the default value is used for each array parameter.


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.