The asort Function

The asort function sorts an array in ascending order while preserving keys. The function modifies the array itself.

Syntax

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

Example

Let's sort an associative array in ascending order by values:

php<?php
  $arr = [
    'b' => 1,
    'e' => 3,
    'c' => 2,
    'a' => 5,
    'd' => 4,
  ];
  
  asort($arr);
  var_dump($arr);
?>

Code execution result:

php[
  'b' => 1,
  'c' => 2,
  'e' => 3,
  'd' => 4,
  'a' => 5,
]

See Also

  • the sort function,
    which sorts by values in ascending order
  • the rsort function,
    which sorts by values in descending order
  • the ksort function,
    which sorts by keys in ascending order
  • the krsort function,
    which sorts by keys in descending order
  • the arsort function,
    which sorts by values in descending order while preserving keys
  • the natsort function,
    which sorts using a natural order algorithm
  • the natcasesort function,
    which sorts using a case-insensitive natural order algorithm
  • the usort function,
    which sorts by values using a user-defined comparison function
  • the uksort function,
    which sorts by keys using a user-defined comparison function
  • the uasort function,
    which sorts by values using a user-defined comparison function while preserving keys
  • the array_multisort function,
    which sorts multiple arrays