PHP array sorting correlation function summary

Source: Internet
Author: User
Tags locale setting shuffle sorts

Array sorting functions include:

Sort (), Rsort (), Asort (), Arsort (), Ksort (), Krsort (), Natsort (), Natcasesort (), Shuffle (), Array_multisort (), Usort (), Uasort (), Uksort ()

Before you differentiate these array sorting functions, you need to clarify several sorting-related points of knowledge:

  1. Some functions are sorted based on the keys of the array , while others are sorted based on values:$array [' key '] = ' value ';.
  2. The array is sorted by the above functions, and the array is persisted after some sort function has been processed by the pre-ordering key and the relational relationship , and cannot be persisted after other functions have been processed.
  3. The sorting rules that you can choose from are:
    1.-Normal comparison unit (does not change type) SORT_REGULAR that is, strings are sorted by string rules, numbers are sorted numerically, and then two types are stitched together (string in front, number behind)
    2.-units are compared as numbers SORT_NUMERIC
    3.-units are compared as strings SORT_STRING
    4.-You can use setlocale () to change the cell as a string comparison based on the current locale setting. SORT_LOCALE_STRING
    5.-Similar to Natsort () sorts the strings in "natural order" for each unit. SORT_NATURAL New in PHP 5.4.0. Natural Sorting: (Implements a sort algorithm that is similar to how people usually sort alphanumeric strings and maintains the association of the original key/value, which is referred to as the "natural sort"), and the specific implementation rules can be consulted:Natsort
    6.-Ability to SORT_STRING merge SORT_NATURAL with or combine (or bitwise), case-insensitive sorting strings. SORT_FLAG_CASE
  4. All sort functions are directly acting on the array itself, rather than returning a new ordered array
  5. All sorting functions are based on an algorithm that is quick to sort and therefore unstable.
function declaration Sort by The array index key remains Order of sorts
bool Sort (array & $array [, int $sort _flags = Sort_regular]) value no low to high
bool Rsort (array & $array [, int $sort _flags = Sort_regular]) value no high to low
bool Asort (array & $array [, int $sort _flags = Sort_regular]) value is
bool Arsort (array &$ Array [, int $sort _flags = Sort_regular]) value high to low
BOOL natsort ( array &$array ) Value Is Natural sort, case sensitive
BOOL natcasesort ( array &$array ) Value Is Natural sort, case insensitive
bool Ksort (array & $array [, int $sort _flags = Sort_regular]) key
bool Krsort (array &$ Array [, int $sort _flags = Sort_regular]) key
BOOL array_multisort ( array &$array1 [, mixed $array1_sort_order = sort_asc [, mixed c14/> = sort_regular [, mixed $... ]]) Value The Association (string) Key name remains the same, but the numeric key name is re-indexed determined by the parameters
BOOL Shuffle ( array &$array ) Value Whether Scrambled Array
bool Usort (array & $array , callable $value _compare_func ) value no user defined
bool Uasort (array & $array , callable $value _compare_func ) value user defined
BOOL uksort ( array &$array , callable $key_compare_func ) Key Is User Defined

Additional points to be added:

    1. Sort (), Rsort (), Shuffle (), Usort () break the association between key and value before sorting
    2. Array_multisort (), in addition to the ability to associate multiple arrays, or to sort a multidimensional array . The array size that participates in the sort is therefore required to be consistent, or the number of multidimensional arrays is equal in each dimension .
1<?PHP2 $ar 1=Array(10, 100, 100, 0);3 $ar 2=Array(1, 3, 2, 4);4 Array_multisort($ar 1,$ar 2);5 Var_dump($ar 1);6 Var_dump($ar 2);7 /***********8 associative sorting, sorted in ascending order of $AR1 regular, $ar 2 on the basis of association with $AR1, sorts the same $ar1 values, different $ar2 values9 Array (4) {Ten [0]=> Int (0) One [1]=> Int (Ten) A [2]=> Int (+) - [3]=> Int (+) - } the Array (4) { - [0]=> Int (4) - [1]=> Int (1) - [2]=> Int (2) + [3]=> Int (3) - } + **********/ A  at $ar=Array( -        Array("Ten", one, one, one, "a"), -        Array(1, 2, "2", 3, 1) -       ); - Array_multisort($ar[0], SORT_ASC, sort_string, -                 $ar[1], Sort_numeric,sort_desc); in Var_dump($ar); - /********* to Array (2) { + [0]=> Array (5) { - [0]=> String (2) "Ten" the [1]=> Int (+) * [2]=> Int (+) $ [3]=> Int (one)Panax Notoginseng [4]=> String (1) "A" -   } the [1]=> Array (5) { + [0]=> Int (1) A [1]=> Int (3) the [2]=> String (1) "2" + [3]=> Int (2) - [4]=> Int (1) $   } $ } - *********/

PHP array sorting correlation function summary

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.