Arrays7 min readExamples run on PHP 8.5.10

asort()

Learn how PHP 8.4 asort() sorts an array by value in ascending order while keeping key association, with flags, edge cases and mistakes to avoid.

Signature

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

Purpose

asort() sorts an array in place by its values, from lowest to highest, and keeps every key attached to its original value. That makes it the right tool for associative arrays where the key carries meaning, such as a name mapped to a score or an ID mapped to a label. If you only care about the values and want fresh numeric keys, sort() is the better fit.

Signature

Parameters and defaults

  • $array is passed by reference, so it must be a variable. The function changes that variable directly and does not build a new array.
  • $flags controls how values are compared. The default is SORT_REGULAR, which compares items with PHP's normal comparison rules.

The supported flags are:

  • SORT_REGULAR compares items normally.
  • SORT_NUMERIC compares items as numbers.
  • SORT_STRING compares items as strings.
  • SORT_LOCALE_STRING compares strings using the current locale, which setlocale() can change.
  • SORT_NATURAL compares strings with natural ordering, like natsort().
  • SORT_FLAG_CASE can be combined with SORT_STRING or SORT_NATURAL using a bitwise OR to ignore case.

Return value and failures

asort() always returns true. Since PHP 8.2 the declared return type is true; earlier versions declared bool. The result is never false, so checking it tells you nothing. The sorted data is in the variable you passed.

The function does have failure modes. Passing something that is not an array raises a TypeError. Passing a literal or a function call result instead of a variable is an error, because the argument cannot be passed by reference.

Two side effects are worth remembering. Elements that compare as equal keep their original relative order, which is guaranteed from PHP 8.0 onward. The array's internal pointer is also reset to the first element.

Example 1: sorting an associative array

The keys follow their values, so the pairs stay intact. The return value is also shown.

Example
<?php
$fruits = ["d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"];
$result = asort($fruits);
echo json_encode($fruits, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
var_dump($result);
Output
{"c":"apple","b":"banana","d":"lemon","a":"orange"}
bool(true)

Example 2: choosing a comparison flag

Numeric strings can sort differently depending on the flag. As strings, "10" comes before "2" because the first character "1" is lower than "2". As numbers, 10 is larger than 2.

Example
<?php
$data = ["a" => "10", "b" => "9", "c" => "2", "d" => "1"];

$asStrings = $data;
asort($asStrings, SORT_STRING);

$asNumbers = $data;
asort($asNumbers, SORT_NUMERIC);

echo json_encode($asStrings), "\n";
echo json_encode($asNumbers), "\n";
Output
{"d":"1","a":"10","c":"2","b":"9"}
{"d":"1","c":"2","b":"9","a":"10"}

Example 3: stable ties and preserved list keys

In the scores below, ana and cy both have 90 and keep their original relative order. A plain list also keeps its old indexes after sorting, so it is no longer a sequential list. Use array_values() when you need keys renumbered from zero.

Example
<?php
$scores = ["ana" => 90, "bo" => 85, "cy" => 90, "di" => 70];
asort($scores);
echo json_encode($scores), "\n";

$list = [30, 10, 20];
asort($list);
echo json_encode($list), "\n";
echo json_encode(array_values($list)), "\n";
Output
{"di":70,"bo":85,"ana":90,"cy":90}
{"1":10,"2":20,"0":30}
[10,20,30]

Edge cases

  • An empty array stays empty, and the function still returns true.
  • Integer keys are preserved just like string keys. The sorted array can look like a list while its keys are out of order, and json_encode() will then output it as an object.
  • Arrays that mix types, such as numbers, numeric strings and non-numeric strings, are compared by the rules of the chosen flag. With SORT_REGULAR the result may be surprising, so pick SORT_NUMERIC or SORT_STRING when the data is mixed.
  • SORT_LOCALE_STRING depends on the current locale, so its results can differ between environments.
  • Only values are compared. Keys never affect the order, except that tied values keep their original sequence.

Common mistakes

  1. Using the return value as the sorted array. $sorted = asort($items); stores true. Sort the variable, then use that same variable.
  2. Expecting renumbered keys. If you want 0, 1, 2, ... after sorting, use sort() or call array_values() on the result.
  3. Forgetting the original order is lost. Because sorting happens in place, copy the array first if you still need the unsorted version, as Example 2 does.
  4. Using the wrong flag. Numeric strings sorted with SORT_STRING give a different order than with SORT_NUMERIC.
  5. Wanting descending order. asort() is ascending only. Use arsort() to sort values from highest to lowest while keeping keys.
  6. Sorting by key. Use ksort() for that. asort() never looks at keys when comparing.
  • sort() sorts values ascending and renumbers keys.
  • arsort() sorts values descending and keeps keys.
  • ksort() sorts by key.
  • uasort() sorts by value with your own comparison callback and keeps keys.
  • natsort() applies natural ordering and keeps keys.

Recap

asort() sorts an array's values in ascending order, in place, and keeps each key paired with its value. It returns true every time, so ignore the return value and use the modified variable. Choose a flag that matches your data, remember that ties keep their original order, and call array_values() if you need sequential keys afterward.

Tested examples

Sorting an associative array by value
<?php
$fruits = ["d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"];
$result = asort($fruits);
echo json_encode($fruits, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
var_dump($result);
Output
{"c":"apple","b":"banana","d":"lemon","a":"orange"}
bool(true)
Choosing between SORT_STRING and SORT_NUMERIC
<?php
$data = ["a" => "10", "b" => "9", "c" => "2", "d" => "1"];

$asStrings = $data;
asort($asStrings, SORT_STRING);

$asNumbers = $data;
asort($asNumbers, SORT_NUMERIC);

echo json_encode($asStrings), "\n";
echo json_encode($asNumbers), "\n";
Output
{"d":"1","a":"10","c":"2","b":"9"}
{"d":"1","c":"2","b":"9","a":"10"}
Stable ties and preserved list keys
<?php
$scores = ["ana" => 90, "bo" => 85, "cy" => 90, "di" => 70];
asort($scores);
echo json_encode($scores), "\n";

$list = [30, 10, 20];
asort($list);
echo json_encode($list), "\n";
echo json_encode(array_values($list)), "\n";
Output
{"di":70,"bo":85,"ana":90,"cy":90}
{"1":10,"2":20,"0":30}
[10,20,30]