Common array functions in php (8) sorting functions (sort, rsort, usort, asort, uasort, arsort, ksort, uksort, krsort, natsort, natcasesort, array_multisort), natcasesort

Source: Internet
Author: User

Common array functions in php (8) sorting functions (sort, rsort, usort, asort, uasort, arsort, ksort, uksort, krsort, natsort, natcasesort, array_multisort), natcasesort

Test environment: php5.3.29

 

sort_flagsYou can use the following values to change the sorting behavior:

Sorting type Tag:

  • SORT_REGULAR -Normal comparison Unit (without changing the type)
  • SORT_NUMERIC -The unit is compared as a number.
  • SORT_STRING -The unit is compared as a string.
  • SORT_LOCALE_STRING -Compare the unit as a string based on the current region (locale) settings. You can use setlocale () to change it.
  • SORT_NATURAL -Similar to natsort (), each unit is sorted in "natural order. Added in PHP 5.4.0.
  • SORT_FLAG_CASE -AbilitySORT_STRING OrSORT_NATURAL Merge (OR bit operation), not case sensitive sorting string. Added in PHP 5.4.0.

 

Sort ($ arr [, $ sort_flags = SORT_REGULAR]

Sort by key value in ascending order. (Note. The key name is deleted and a digital index is created. You can use the asort function to retain the key name)

(The elements with the same value are actually exchanged in the sorting function. Because the values are the same, they cannot be seen but are still exchanged .)

Rsort (same as above)

Sort by key value in descending order. (Note. The same as above will also delete the key name, you need to retain the key name can use the arsort function)

Usort ($ arr, $ cmp_function)

The custom function is sorted by key. (Note. The same as the key name is missing. You can use the uasort function to reserve the key name)

The two parameters of cmp_function ($ a, $ B) are key values. Return a positive value. Set parameter 1 to the end of parameter 0. negative value to the end of parameter 2.

Asort ($ arr [, $ sort_flags = SORT_REGULAR ))

Like sort, except for the reserved key-name association.

Arsort (same as above)

Like rsort, except for retaining key-name associations.

Uasort ($ arr, $ cmp_function)

Same as usort. In addition to retaining the association of key names.

Ksort ($ arr [, $ sort_flags = SORT_REGULAR ))

Sort by key name in ascending order.

Krsort (same as above)

Sort by key name in descending order.

Uksort ($ arr, $ cmp_function)

The two parameters of $ cmp_function ($ a, $ B) are key names. The return process is the same as that of usort.

Natsort ($ arr)

"Natural (natural) sorting" is in ascending order of values.

1. For example, 2 is before 13 in ascending order.

2. Case sensitivity. For example, the element 'img1' img2' img3' is sorted as Img3 img1 img2.

Natcasesort ($ arr)

Natsort ($ arr) ignores the case-sensitive version.

Array_multisort ($ arr1 [, $ arr2. .. $ arrN, $ sort_flags = SORT_REGULAR)

The join (string) key name remains unchanged, and the numeric index is regenerated.

Set $ arr2 ~ The elements in the n array are moved in the same order as those in $ arr1.

For example, $ arr1 = array (2, 3, 1 );

Changed to array (1, 2, 3 );

$ Arr2 ~ N also places the subscript 0 on subscript 1; the subscript 1 element on subscript 2; and The subscript 2 to the subscript 0;

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.