Signature
asort(array &$array, int $flags = SORT_REGULAR): truePurpose
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
$arrayis passed by reference, so it must be a variable. The function changes that variable directly and does not build a new array.$flagscontrols how values are compared. The default isSORT_REGULAR, which compares items with PHP's normal comparison rules.
The supported flags are:
SORT_REGULARcompares items normally.SORT_NUMERICcompares items as numbers.SORT_STRINGcompares items as strings.SORT_LOCALE_STRINGcompares strings using the current locale, whichsetlocale()can change.SORT_NATURALcompares strings with natural ordering, likenatsort().SORT_FLAG_CASEcan be combined withSORT_STRINGorSORT_NATURALusing 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.
$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);{"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.
$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";{"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.
$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";{"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_REGULARthe result may be surprising, so pickSORT_NUMERICorSORT_STRINGwhen the data is mixed. SORT_LOCALE_STRINGdepends 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
- Using the return value as the sorted array.
$sorted = asort($items);storestrue. Sort the variable, then use that same variable. - Expecting renumbered keys. If you want
0, 1, 2, ...after sorting, usesort()or callarray_values()on the result. - 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.
- Using the wrong flag. Numeric strings sorted with
SORT_STRINGgive a different order than withSORT_NUMERIC. - Wanting descending order.
asort()is ascending only. Usearsort()to sort values from highest to lowest while keeping keys. - Sorting by key. Use
ksort()for that.asort()never looks at keys when comparing.
Related functions
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
$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);{"c":"apple","b":"banana","d":"lemon","a":"orange"}
bool(true)
$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";{"d":"1","a":"10","c":"2","b":"9"}
{"d":"1","c":"2","b":"9","a":"10"}
$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";{"di":70,"bo":85,"ana":90,"cy":90}
{"1":10,"2":20,"0":30}
[10,20,30]