Sorting functions in PHP Sort, asort, Rsort, Krsort, Ksort difference analysis

Source: Internet
Author: User
Tags locale setting

With a lot of array sorting functions in PHP, let's hit about the use of PHP array sorting.

The sort () function is used to sort the array cells from low to high.
The Rsort () function is used to sort the array cells from high to low.
The Asort () function is used to sort the array cells from low to high and keep the index relationship.
The Arsort () function is used to sort the array cells from high to low and keep the index relationship.
The Ksort () function is used to sort the array cells from low to high by their key names.
The Krsort () function is used to sort the array cells by their key names from high to low.

Sort ()

The PHP sort () function is used to sort the array cells from low to high, and returns TRUE if successful, and FALSE if it fails.
Note: This function assigns a new key name to the cells in the sorted array, which removes the existing key names and not just the reordering.
Grammar:
BOOL Sort (array &array [, int sort_flags])
The optional parameter sort_flags is used to change the ordering behavior:
Sort_flags Value Description
Sort_regular Normal comparison unit
The Sort_numeric unit is compared as a number.
The sort_string unit is compared as a string.
Sort_locale_string the cell as a string compared to the current locale setting

Example:

?
12345 $arr= array("b", "a", "c");sort($arr);print_r($arr);?>

Run the example output:

?
1 Array ( [0] => a [1] => b [2] => c )

In this case, the $arr array cells are sorted alphabetically, and the array cells are sorted, and the key values are reassigned.

Rsort ()
The PHP Rsort () function behaves in contrast to sort (), where the array cells are sorted from high to low, please refer to the sort () function.

Asort ()
The PHP Asort () function is used to sort the array cells from low to high and maintain an index relationship, and if successful returns TRUE, the failure returns FALSE.
Grammar:
BOOL Asort (array &array [, int sort_flags])
The optional parameter sort_flags is used to change the ordering behavior, see sort () in detail.
Example:
  

?
1234 $arr= array("b", "a", "c");asort($arr);print_r($arr);?>

Run the example output:

?
1 Array ( [1] => a [0] => b [2] => c )

Arsort ()
The PHP Arsort () function behaves in contrast to Asort (), where the array cells are sorted from high to low and maintain an index relationship, refer to the Asort () function.

Ksort ()
The PHP Ksort () function is used to sort the array cells by the key name from low to high and returns TRUE if successful, and FALSE if it fails.
This function retains the original key name, so it is often used for associative arrays.
Grammar:
BOOL Ksort (array &array [, int sort_flags])
The optional parameter sort_flags is used to change the ordering behavior, see sort () in detail.
Example:
  
  

?
1234 $arr= array("b"=>18, "a"=>20, "c"=>25);  ksort($arr);  print_r($arr);  ?>

Run the example output:

?
1 Array ( [a] => 20 [b] => 18 [c] => 25 )

  Krsort ()
The PHP Krsort () function behaves in contrast to Ksort (), and the array cells are sorted from highest to lowest by key name, please refer to the Ksort () function.

Sorting functions in PHP Sort, asort, Rsort, Krsort, Ksort difference analysis

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.