asort

Sorts an array in place by value in ascending order while keeping each value bound to its key.

Signature
asort(array &$array, int $flags = SORT_REGULAR): true
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$arrayarrayThe array to sort. Passed by reference and modified in place.
$flagsoptionalint= SORT_REGULARComparison mode: SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING or SORT_NATURAL, optionally OR-ed with SORT_FLAG_CASE.

Return value

trueAlways returns true. Since PHP 8.2.0 the declared return type is true; before that it was bool.

Changelog

  • PHP 8.2.0The return type is true now; previously it was bool.
  • PHP 8.0.0Sorting is stable: elements that compare as equal keep their original relative order.

asort sorts an array from smallest to largest by value and keeps every key attached to the value it started with. It is the ascending twin of arsort, and the key-preserving alternative to sort.

When to reach for it

Use it whenever the key is a label you still need after sorting: country code to country name, user id to score, SKU to price. A dropdown built from an id => label map is the everyday case — asort puts the labels in alphabetical order without breaking the ids.

What trips people up

Like every PHP sort function, asort works in place and returns true. Writing $sorted = asort($data) silently replaces your array with a boolean — one of the most-reported non-bugs in PHP.

The other trap is SORT_REGULAR on mixed data. Comparing '10' against '9' under the default flag uses numeric comparison because both are numeric strings, but '10' against 'apple' falls back to a string comparison. Declare your intent with SORT_STRING or SORT_NUMERIC when the array is uniform.

Because keys survive, an asort-ed array is often no longer a list — json_encode will emit a JSON object rather than an array. Run array_values first if the output must be a JSON array.

asort also resets the internal array pointer.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Alphabetise an id => name map for a select box without losing the ids.
  • Order products by price ascending while keeping SKUs as keys.
  • Sort a country-code => country-name lookup table for display.

Frequently asked questions

What is the difference between sort and asort?

sort discards the keys and renumbers the array from zero, while asort keeps each key attached to its value. Use asort for associative arrays where the key still means something after sorting.

Does asort return the sorted array?

No. It sorts the array in place through a reference parameter and always returns true, so you must call it as a statement rather than assigning its result.

How do I sort an associative array by key instead of value?

Use ksort for ascending key order or krsort for descending. asort and arsort only ever look at values.

More examples

Sorted keys are not JSON-array friendly

The first line prints {"1":10,"2":30,"0":40} because asort kept the original integer keys, which are no longer 0,1,2 in order. array_values renumbers them so the second line prints the expected [10,30,40].

Example · php
<?php
$scores = [40, 10, 30];

asort($scores);
echo json_encode($scores), "\n";                // object: keys survived
echo json_encode(array_values($scores)), "\n";  // array: keys dropped

Choosing the right comparison flag

SORT_STRING compares character by character, so '100' lands between '10' and '9'. SORT_NUMERIC casts each value to a number first and gives the ordering a human expects from sizes.

Example · php
<?php
$sizes = ['s' => '10', 'm' => '9', 'l' => '100'];

$asString = $sizes;
asort($asString, SORT_STRING);
asort($sizes, SORT_NUMERIC);

echo implode(',', $asString), "\n"; // 10,100,9
echo implode(',', $sizes), "\n";   // 9,10,100

Stable ordering of ties

Both nines and both twelves tie, and PHP 8's stable sort keeps each pair in insertion order, printing bob, di, ada, cy. On PHP 7 the order within a tie was undefined and could differ between runs or array sizes.

Example · php
<?php
$times = ['ada' => 12, 'bob' => 9, 'cy' => 12, 'di' => 9];

asort($times);

echo implode(', ', array_keys($times)), "\n";

Discussion

  • Be the first to comment on this lesson.