PHP asort function

Last Updated: February 17, 2022

asort — Sort an array in ascending order according to the value.

Syntax

asort(array &$array, int $flags = SORT_REGULAR): bool

Supports (PHP 4 , PHP 5, PHP 7, PHP 8)

Parameters

Parameter Description
array target array
flags

Optional value is used to change the sorting behavior

  • SORT_REGULAR – compare items normally;
  • SORT_NUMERIC – compare items numerically
  • SORT_STRING – compare items as strings
  • SORT_LOCALE_STRING – compare items as strings
  • SORT_NATURAL – compare items as strings using “natural ordering” like natsort()
  • SORT_FLAG_CASE – can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

Return Value

Returns true always

Example

$array = array("one" => "One", "two" => "Two",'three'=>"Three");
asort($array);

foreach ($array as $key => $val) {
    echo "$key = $val" ."<br>";
}

Output

two = Two
three = Three
one = One
Test