arsort

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

Signature
arsort(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.

arsort sorts an array from largest to smallest by value, and — unlike rsort — it carries each key along with its value. That key preservation is the whole point: it is the function you use when the key identifies the thing and the value ranks it.

When to reach for it

Leaderboards, word-frequency counts, tag clouds, "top N" reports. The classic pairing is arsort(array_count_values($words)) followed by array_slice($words, 0, 10, true) to take the ten most common entries with their labels intact.

What trips people up

The function sorts in place and returns true, so $sorted = arsort($data) replaces your array with a boolean. Always call it as a statement.

The default SORT_REGULAR compares mixed types using PHP's standard rules, which can surprise you when numeric strings and real numbers are mixed. Pass SORT_NUMERIC or SORT_STRING when the array is known to be homogeneous — it is both clearer and faster.

Since PHP 8.0 the sort is stable, so ties keep their insertion order rather than shuffling unpredictably. Code written against older versions that worked around instability can usually be simplified.

Finally, arsort resets the internal array pointer, so any position you had established with next or end is lost.

Example

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

When to use it

  • Rank players by score for a leaderboard while keeping player names as keys.
  • Show the ten most frequent words after array_count_values.
  • Order a per-category sales total map from best-selling to worst.

Frequently asked questions

What is the difference between arsort and rsort?

arsort keeps the key/value association intact, while rsort discards the keys and renumbers the array from zero. Use arsort for associative arrays and rsort when you just want a reverse-sorted list.

How do I sort an array by value descending and keep the keys?

Call arsort($array) — it sorts in place, descending by value, with keys preserved. Do not assign the return value; it is always true.

Is arsort stable for equal values?

Yes, since PHP 8.0. Elements that compare as equal retain the order they had in the original array; before PHP 8.0 their relative order was undefined.

More examples

Top three words in a sentence

array_count_values builds word => count, arsort ranks it, and array_slice with preserve_keys set to true takes the top three without renumbering. The output is the => 3, cat => 2 and one of the single-occurrence words, whose original order is preserved because PHP 8 sorts stably.

Example · php
<?php
$text  = 'the cat sat on the mat the cat purred';
$words = array_count_values(explode(' ', $text));

arsort($words);

print_r(array_slice($words, 0, 3, true));

Natural, case-insensitive descending order

SORT_NATURAL treats the digit runs as numbers, so img12 outranks IMG2, and SORT_FLAG_CASE stops the uppercase name from sorting separately. The result is img12.png, IMG2.png, img1.png with keys a, b, c still attached.

Example · php
<?php
$files = ['a' => 'img12.png', 'b' => 'IMG2.png', 'c' => 'img1.png'];

arsort($files, SORT_NATURAL | SORT_FLAG_CASE);

print_r($files);

arsort versus rsort

Both produce the values in the order 15, 12, 9, but arsort keeps bob, cy and ada as keys while rsort discards them and renumbers from 0. Pick rsort only when the keys genuinely carry no meaning.

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

$copy = $scores;
arsort($scores);
rsort($copy);

print_r($scores); // keys kept
print_r($copy);   // keys thrown away

Discussion

  • Be the first to comment on this lesson.