The sort() function in PHP sorts an array in ascending order.
Syntax
sort(arr, flag)
Parameters
arr − The array to sort.
flag −
0 = SORT_REGULAR − Default. Compare items normally. Do not change types.
1 = SORT_NUMERIC − Compare items numerically
2 = SORT_STRING − Compare items as strings
3 = SORT_LOCALE_STRING − Compare items as strings, based on current locale
4 = SORT_NATURAL − Compare items as strings using natural ordering.
Return
The sort() function sorts the array. It returns TRUE on success and FALSE on failure.
Example
The following is an example −
<?php $arr = array("jack", "tim", "scarlett","david" ); sort($arr, SORT_STRING); print_r($arr); ?>
Output
Array ( [0] => david [1] => jack [2] => scarlett [3] => tim )
Let us see another example −
Example
<?php $arr = array(45, 87, 23, 66, 12, 98, 56); sort($arr); print_r($arr); ?>
Output
Array ( [0] => 12 [1] => 23 [2] => 45 [3] => 56 [4] => 66 [5] => 87 [6] => 98 )