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