Facebook Page Twitter Page LinkedIn Page
× PHP Tutorial PHP Advanced PHP Resources


The PHP Array sort() function is used to sort an indexed array in ascending order. The function has one optional parameter which can be used to specify sorting type. Please see below for more details.

Syntax

sort(array, sorting_type)

Parameters

array Required. Specify the indexed array to sort
sorting_type Optional. There are six sorting types which are mentioned below:

  • 0 or SORT_REGULAR - Default. Compare items normally.
  • 1 or SORT_NUMERIC - Compare items numerically.
  • 2 or SORT_STRING - Compare items as strings.
  • 3 or SORT_LOCALE_STRING - Compare items as strings, based on current locale.
  • 4 or SORT_NATURAL - Compare items as strings using natural ordering.
  • 5 or SORT_FLAG_CASE - Compare items as strings and case-insensitive.

Note: The | (bitwise operator) can be used to specify more than one sorting type, for example - 4|5.


Return Value

Returns TRUE on success and False in failure.

Exceptions

NA.

Example:

In the below example, sort() function is used to sort an indexed array called MyArray in ascending order.

<?php
$MyArray = array("Marry", "John", "Jo", "Kim", "Adam");
sort($MyArray);

print_r($MyArray);
?>

The output of the above code will be:

Array
(
    [0] => Adam
    [1] => Jo
    [2] => John
    [3] => Kim
    [4] => Marry
)

❮ PHP Array functions