PHP Array Sorting
Elements in an array can be sorted in ascending or descending order, alphabetically or numerically.
PHP - Array Sorting Functions
In this chapter, we will introduce the following PHP array sorting functions one by one:
- sort() - Sort arrays in ascending order
- rsort() - Sort arrays in descending order
- asort() - Sort associative arrays in ascending order, according to the value
- ksort() - Sort associative arrays in ascending order, according to the key
- arsort() - Sort associative arrays in descending order, according to the value
- krsort() - Sort associative arrays in descending order, according to the key
sort() - Sort arrays in ascending order
The following example sorts the elements of the $cars array in alphabetical ascending order:
The following example sorts the elements of the $numbers array in numerical ascending order:
rsort() - Sort arrays in descending order
The following example sorts the elements of the $cars array in alphabetical descending order:
The following example sorts the elements of the $numbers array in numerical descending order:
asort() - Sort associative arrays in ascending order, according to the value
The following example sorts an associative array in ascending order, according to the value:
ksort() - Sort associative arrays in ascending order, according to the key
The following example sorts an associative array in ascending order, according to the key:
arsort() - Sort associative arrays in descending order, according to the value
The following example sorts an associative array in descending order, according to the value:
krsort() - Sort associative arrays in descending order, according to the key
The following example sorts an associative array in descending order, according to the key:
Complete PHP Array Reference
For a complete reference of all array functions, visit ourPHP Array Reference。
This reference manual provides a brief description and application examples for each function!
Other Extensions