Differences between PHP sorting functions sort, asort, rsort, krsort, and ksort

Source: Internet
Author: User
In php, the sort, asort, rsort, krsort, and ksort functions are provided for simple data sorting, next I will give you a brief introduction to sort, asort, rsort, krsort, and ksort functions... in php, the sort, asort, rsort, krsort, and ksort functions are provided for simple data sorting, next I will give you a brief introduction to the differences between sort, asort, rsort, krsort, and ksort functions.

A large number of array sorting functions are introduced in php. next we will introduce the usage of php array sorting.

The sort () function is used to sort array units from low to high.

The rsort () function is used to sort array units from high to low.

The asort () function is used to sort array units from low to high and maintain the index relationship.

Arsort () is used to sort array units from high to low and maintain the index relationship.

The ksort () function is used to sort array units by key names from low to high.

The krsort () function is used to sort array units by key names from high to low.

Sort ()

The PHP sort () function is used to sort array units from low to high. If the array unit is successfully sorted, TRUE is returned. If the array unit fails, FALSE is returned.

Note: This function will assign a new key name to the unit in the sorted array, which will delete the original key name, not just the reorder.

Syntax:

Bool sort (array & array [, int sort_flags])

The optional parameter sort_flags is used to change the sorting 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 compare the unit as a string based on the current region (locale) settings

Example:

 

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

In this example, the $ arr array units are sorted alphabetically, and the array units are sorted and the key values are re-allocated.

Rsort ()

The PHP rsort () function is opposite to sort (). sort the array units in descending order. For more information, see sort () function.

Asort ()

The PHP asort () function is used to sort array units from low to high and maintain the index relationship. if it succeeds, TRUE is returned, and FALSE is returned if it fails.

Syntax:

Bool asort (array & array [, int sort_flags])

The optional parameter sort_flags is used to change the sorting behavior. for details, see sort ().

Example:

 

Run the sample output:

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

Arsort ()

The PHP arsort () function is opposite to asort (). It sorts array units from high to low and maintains the index relationship. For more information, see use the asort () function.

Ksort ()

The PHP ksort () function is used to sort array cells by key names from low to high. if the key name is successful, TRUE is returned. if the key name fails, FALSE is returned.

This function retains the original key name, so it is often used to associate arrays.

Syntax:

bool ksort( array &array [, int sort_flags] )

The optional parameter sort_flags is used to change the sorting behavior. for details, see sort ().

Example:

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

Run the sample output:

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

Krsort ()

The behavior of the PHP krsort () function is opposite to that of ksort (). sort the array elements by key name from high to low. for details, refer to the ksort () function usage.


Address:

Reprinted at will, but please attach the article address :-)

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.