PHP array sorting function: Different for sort, asort, and Ksort

Source: Internet
Author: User

When doing PHP development, it is often necessary to sort the arrays, and three functions are often used in sorting a one-dimensional array: Sort, asort, and Ksort.

As you all know, sort () and asort () are all sorting values, except that when sort () is sorted, the key is removed, and asort () retains the key. Such as:

  $a = array (' A ' = ' and ' C ', ' B ' = ' B ', ' c ' = = ' d ', ' d ' = ' a ' 

The output is:

Array (4) {["D"]=> string (1)" A " [" B "]=> string (1)" B " [" a "]=> string (1)" C " [" C "]=> string (1)" D "
}

and

  $a = array (' A ' = ' and ' C ', ' B ' = ' B ', ' c ' = = ' d ', ' d ' = ' a ' 

output is:

 array (4) {
[0]=> string (1)" A "
[1]=> string (1)" B "
[2]=> string (1)" C "
[3]=> string (1)" D "
}

and Ksort () differs from the above in that the function is sorted by key.

  $a = array (' C ' = ' A ', ' b ' = ' d ', ' d ' = ' C ', ' a ' = ' B ' 

output is:

 array (4) {
["a"]=> string (1)" B "
[" B "]=> string (1)" D "
[" C "]=> string (1)" A "
[" D "]=> string (1)" C "}

However, there is a difference between ksort () and the first two sorts that may be overlooked:

When the numbers and characters are mixed:

$a =Array (' 1 ' = ' 1 ', ' 2 ' = ' 2 ', ' d ' = = ' d ', ' a ' = = ' a ');Ksort ($avar_dump ( $a asort ( $a var_dump ( $a);         

Output Result:

Array (4) {["a"]=>String (1) "a"["D"]=>String (1) "D"[1]=>String (1) "1"[2]=>String (1) "2"}Array (4) {[1]=>string ( 1) "1"  [2]=> string (1)" 2 " [" a "]=> string (1)" A " [" D "]=> string (1)" D "}        

The sorting results are different. It seems ksort that letters should be in front of numbers, and asort that letters should be behind numbers. Reading the PHP document will reveal that all three functions have a second parameter:sorttype

Parameters Description
Array Necessary. The input array.
sorttype

optional. Specifies how the values of the array are arranged. Possible values:

  • sort_regular-default. Processed in their original type (without changing the type).
  • sort_numeric-handling values as numbers
  • sort_string-handles the value as a string for
  • sort_locale_string-handles the value as a string, based on the local settings *.

Generally we do not pass in the second parameter, but it is necessary to consider the case where the numbers and letters are mixed. For Asort, we sort by value, the example four elements are string type, so Sort_regular-by default will press Sort_string-the value as a string to handle. For Ksort, although our keys are strings, PHP automatically converts string-type numeric keys to int, so when sorting sort_regular-the default is to press Sort_numeric-to handle the value as a number. Sort differently. Of course, the result is different. We can verify that:

$a =Array (' a ' = = ' A ', ' b ' = = ' B ', ' 1 ' =>1, ' 2 ' =>2);Ksort ($avar_dump ( $a asort ( $a var_dump ( $a);         

Output Result:

Array (4) {
["A"]=>String (1) "a"
["B"]=>string ( 1) "B"
[1]=> int (1)
[2]=> Int (2) BR style= "margin:0px; padding:0px ">
array (4) {
[" a "]=> string (1)" A "
[" B "]=> string (1)" B "
[1]=> int (1)
[2]=> int (2)
}

So the world will be peace

When using these functions, it is necessary to pass in the second parameter. However, it is hoped that the future will not encounter the need to sort together letters and numbers such as the anti-human situation-no one knows which to put on the front ~

PHP array sorting function: Different for sort, asort, and Ksort

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.