PHP array sorting function Sort,rsort,ksort,krsort,asort,arsort difference

Source: Internet
Author: User
Tags sorts

First, the concept:
1, natural sorting method: that is, according to the natural number of the size of the order, such as "10>2";
2, the computer standard sorting method: is to compare by character, sort, for example "2>10" (because the first bit 2>1);
3, numeric array: An array with a numeric ID key;
4. Associative array: An array with the specified key, with each key associated with a value.
5, the default sorting for key values in ascending order, sorted by the natural sort.

Second, "Help understanding"
1, A (association) prefix is a key value associated with the ordering;
2, the K (key) prefix indicates the sort by the key name association;
3, R (reverse) prefixes are ordered in the opposite direction.

Third, sort
Example:

1     $arr=Array("Ben" = "2", "Peter" and "Ten", "Joe" = "30");2     Echo"-------------echo array-------------<br>";3     foreach($arr  as $key=$value) {4         #code ...5         Echo"Key=".$key. ", value=."$value." <br> ";6}

1. Array of sorted values:
1), sort: The default sort, that is, the key value in ascending order, because the function is used to sort a one-dimensional array, all the sorted key names will be re-defined, the associative array is sorted by this method, the key name of the associative array is not preserved, instead of the numeric key.
2), Rsort: This function is similar to the sort function usage, and the difference is that the key values are sorted in reverse order.

1     Echo"-------------sorts by sort-------------<br>";2     Sort($arr);3     foreach($arr  as $key=$value) {4         #code ...5         Echo"Key=".$key. ", value=."$value." <br> ";6     }7 8     Echo"-------------sorts by Rsort-------------<br>";9     Rsort($arr);Ten     foreach($arr  as $key=$value) { One         #code ... A         Echo"Key=".$key. ", value=."$value." <br> "; -}

Output Result:
-------------echo Array-------------
key= ben,value= 2
key= peter,value= 10
key= joe,value= 30
-------------Sorts by sort-------------
key= 0,value= 2
key= 1,value= 10
key= 2,value= 30
-------------Sorts by Rsort-------------
key= 0,value= 30
key= 1,value= 10
key= 2,value= 2

2. Sort Associative arrays:
The ordering of associative arrays has two sort items, the first is sorted by the key names of the arrays, and the second is sorted by the key values of the arrays.
Sort by key name:
1), Ksort: Sort by the key name of the associative array, and the result of the ordering preserves the association relationship of the key value.
2), Krsort: Sorted by the key name of the associative array, and the result of the ordering preserves the association relationship of the key value.

1     Echo"-------------sorts by Ksort--------------<br>";2     Ksort($arr);3     foreach($arr  as $key=$value) {4         #code ...5         Echo"Key=".$key. ", value=."$value." <br> ";6     }7 8     Echo"-------------sorts by Krsort-------------<br>";9     Krsort($arr);Ten     foreach($arr  as $key=$value) { One         #code ... A         Echo"Key=".$key. ", value=."$value." <br> "; -}

Output Result:
-------------echo Array-------------
key= ben,value= 2
key= peter,value= 10
key= joe,value= 30
-------------Sorts by Ksort--------------
key= ben,value= 2
key= joe,value= 30
key= peter,value= 10
-------------Sorts by Krsort-------------
key= peter,value= 10
key= joe,value= 30
key= ben,value= 2

  Sort Key values:
1), Asort: Sort by the key value of the associative array, and the result of the order preserves the association relationship of the key value
2), Arsort: Sorted by the key value of the associative array, the result of the ordering preserves the relationship of the key value.

1     Echo"-------------sorts by Asort-------------<br>";2     Asort($arr);3     foreach($arr  as $key=$value) {4         #code ...5         Echo"Key=".$key. ", value=."$value." <br> ";6     }7 8     Echo"-------------sorts by Arsort-------------<br>";9     Arsort($arr);Ten     foreach($arr  as $key=$value) { One         #code ... A         Echo"Key=".$key. ", value=."$value." <br> "; -}

Output Result:
-------------echo Array-------------
key= ben,value= 2
key= peter,value= 10
key= joe,value= 30
-------------Sorts by Asort-------------
key= ben,value= 2
key= peter,value= 10
key= joe,value= 30
-------------Sorts by Arsort-------------
key= joe,value= 30
key= peter,value= 10
key= ben,value= 2

PHP array sorting function Sort,rsort,ksort,krsort,asort,arsort difference

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.