In this chapter, we will cover the following PHP array sorting functions:
sort()
: sorts arrays in ascending orderrsort()
: sorts arrays in descending orderasort()
: sorts associative arrays in ascending order based on the valueksort()
: sorts associative arrays in ascending order based on the keyarsort()
: sorts associative arrays in descending order based on the valuekrsort()
: sorts associative arrays in descending order based on the keyThe following example sorts the elements of the $cars array in ascending alphabetical order:
$cars |
The following example sorts the elements of the $numbers array in ascending numerical order:
$cars |
The following example sorts the elements of the $numbers
array in ascending order numerically:
$numbers |
The following example sorts the elements of the $cars array in descending alphabetical order:
$cars |
The following example sorts the elements of the $numbers
array in descending numerical order:
$numbers |
The following example sorts an associative array in ascending order based on the value:
$age
|
The following example sorts an associative array in ascending order based on the key:
$age |
The following example sorts an associative array in descending order based on the value:
$age |
The following example sorts an associative array in descending order based on the key:
$age |