PHP--Array sorting

Source: Internet
Author: User
Tags sorts

PHP Array sorting function:

Sort ()-ascending order of arrays

Rsort ()-Descending order of the array

Asort ()-Sets the array in ascending order based on the values of the associative arrays

Ksort ()-an array is sorted in ascending order based on the keys of the associative arrays

Arsort ()-sorts arrays in descending order based on the values of the associative array

Krsort ()-Sorts the arrays in descending order based on the keys of the associative array

These functions are directly manipulating the original array, modifying the array directly

Sort ()-ascending order of arrays

1, the elements in the $cars array in ascending alphabetical order:

<? PHP $cars=array("Volvo", "BMW", "Toyota"); Sort ($cars);? >

Results:

BMW
Toyota
Volvo

2, the elements in the $numbers array in ascending order of numbers

<? PHP $numbers=array(4,6,2,22,11); Sort ($numbers);? >

Results:

2
4
6
11
22

Rsort ()-Descending order of the array

Just the opposite of sort ()

1. Arrange the elements in the $cars array in descending alphabetical order

2. Sort the elements in the $numbers array in descending order of numbers

Ksort ()-Sorts the array in ascending order based on the key

The associative array is sorted in ascending order based on the key of the array

<? PHP $age=array("Peter" and "+", "Ben" = "Notoginseng", "Joe" and "" "); Ksort ($age);? >

Results:

Key=ben, value=37
Key=joe, value=43
Key=peter, value=35

First B and then J again p

Krsort ()-descending order of arrays based on the keys of the array

The associative array is sorted in descending order based on the key of the array

<? PHP $age=array("Peter" and "+", "Ben" = "Notoginseng", "Joe" and "" "); Krsort ($age);? >

Results:

Key=peter, value=35
Key=joe, value=43
Key=ben, value=37

Asort ()-Sorts the array in ascending order based on the values of the arrays

The associative array is sorted in ascending order based on the value of the array

<? PHP $age=array("Peter" and "+", "Ben" = "Notoginseng", "Joe" and "" "); Asort ($age);? >

Results:

Key=peter, value=35
Key=ben, value=37
Key=joe, value=43

Arsort ()-sorts arrays in descending order based on the values of the array

The associative array is sorted in descending order based on the value of the array

<? PHP $age=array("Peter" and "+", "Ben" = "Notoginseng", "Joe" and "" "); Arsort ($age);? >

Results:

Key=joe, value=43
Key=ben, value=37
Key=peter, value=35

PHP--Array sorting

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.