PHP Array sort() Function
Last Updated : 17 Mar 2025
The PHP sort( ) function is used to sort the array elements in ascending order. This function was introduced in PHP 4.0.
Syntax
Parameter
| Parameter | Description | Is compulsory |
|---|---|---|
| array | Specifies the array to be sorted. | compulsory |
| Sort flags | Sorting behavior. Possible type : SORT_REGULAR - Compare items normally. SORT_NUMERIC - Compare items numerically. SORT_STRING - Compare items as strings. SORT_LOCALE_STRING - compare items as strings, based on the current locale. | Optional |
Returns
The sort( ) function returns true on success or false on failure.
Example 1
Output:
Array
(
[0] => acoustic
[1] => classical
[2] => electric
)
Example 2
Output:
Array
(
[0] =>audi
[1] =>bmw
[2] =>nissan
)
Example 3
Output:
Array
(
[0] => 400
[1] => 100
[2] => 50
[3] => 20
[4] => 1
)
Array
(
[0] => 1
[1] => 20
[2] => 50
[3] => 100
[4] => 400
)
Example 4
Output:
Array
(
[0] => compiler
[1] =>dbms
[2] => networking
[3] =>os
)
Next TopicPhp-array-splice-function