PHP sort() Function

PHP sort Function


Definition :
The sort() is inbuilt function in PHP Array.Basically sort() function is used to sorts the values of the indexed array in ascending order.
Syntax
 
The basic syntax of the sort() function is given with:
      sort(arraysort_flags);
 
Parameters
The sort() function accepts the following parameters.
Parameter
Description
array
Required. Specifies the array to sort.
sort_flags
Optional. Specifies how array items should be compared. Possible values are:
  • SORT_REGULAR – Compare items normally (don't change types). Default value.
  • SORT_NUMERIC – Compare items numerically.
  • SORT_STRING – Compare items as strings.
  • SORT_LOCALE_STRING – Compare items as strings, based on the current locale.
  • SORT_NATURAL – Compare items as strings using natural ordering.
  • SORT_FLAG_CASE – Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.
Example1 : Given below example Sort the elements of the $website array in ascending alphabetical order.

Output : 

Array
(
    [0] => Designing
    [1] => House
    [2] => Online
    [3] => Promotion
    [4] => Web
)

Example2 : following example sorts an indexed array having numeric values in ascending order

Output :

Array
(
    [0] => 2
    [1] => 5
    [2] => 6
    [3] => 8
)