sort()
sort() — Sort an array in ascending order.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
sort(array &$array, int $flags = SORT_REGULAR): true
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| &$array | array | The input array. |
| $flags | int | Sorting type flags: SORT_REGULAR - compare items normally; the details are described in the comparison operators section SORT_NUMERIC - compare items numerically SORT_STRING - compare items as strings SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale() SORT_NATURAL - compare items as strings using "natural ordering" like natsort() ... |
Return value
Always returns true.
Example
A runnable snippet:
$fruits = array("lemon", "orange", "banana", "apple"); sort($fruits); foreach ($fruits as $key => $val) { echo "fruits[" . $key . "] = " . $val . "\n"; }
Output:
fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange
Changelog
| Version | Change |
|---|---|
| 8.2.0 | The return type is true now; previously, it was bool. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page sort() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.