Arrays7 min readExamples run on PHP 8.5.10

arsort()

Learn how PHP 8.4 arsort() sorts an array by value from high to low while preserving keys, with flags, ties, edge cases and mistakes.

Signature

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

Purpose

arsort() sorts an array in place by its values, from largest to smallest, and keeps every key attached to its value. The name reads as "associative reverse sort". It is the right tool for leaderboards, vote counts, word frequencies and any other keyed data where the order matters but the keys must survive.

If you do not care about keys, rsort() gives the same value order but renumbers the keys. If you want to sort by key instead, look at krsort().

Signature

Parameters and defaults

  • $array is taken by reference, so the original variable is modified. You must pass a variable, not a literal array or a function result.
  • $flags changes how values are compared. The default is SORT_REGULAR, which compares values 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 using natural ordering, like natsort().
  • SORT_FLAG_CASE can be combined with SORT_STRING or SORT_NATURAL using | for case-insensitive comparison.

Return value and failure cases

The function always returns true. Since PHP 8.2 the declared return type is true; before that it was documented as bool. The result is never a sorted copy: the sorted data lives in the variable you passed. Passing something that is not an array raises a TypeError.

Sorting also resets the array's internal pointer to the first element.

Example 1: a basic leaderboard

The keys are names and the values are scores. After sorting, the highest score comes first and each name still points to its own score.

Example
<?php
$scores = ["ana" => 82, "ben" => 95, "cy" => 70];
arsort($scores);
echo json_encode($scores, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"ben":95,"ana":82,"cy":70}

Example 2: ties and the return value

When two values compare as equal, they keep their original relative order. This is guaranteed since PHP 8.0; earlier versions left it undefined. In the example below, green was before gold in the input and stays before it, and red stays before blue.

Example
<?php
$votes = ["red" => 3, "green" => 5, "blue" => 3, "gold" => 5];
$result = arsort($votes);
var_dump($result);
echo json_encode($votes, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
bool(true)
{"green":5,"gold":5,"red":3,"blue":3}

Example 3: choosing a flag

The same numeric-looking strings sort differently depending on the flag. With the default flag, PHP treats numeric strings as numbers, so "100" is largest. With SORT_STRING, the comparison is character by character, so "9" beats "100", and "100" beats "10" because it is longer with the same prefix.

Example
<?php
$items = ["a" => "10", "b" => "9", "c" => "100"];
$regular = $items;
$text = $items;
arsort($regular);
arsort($text, SORT_STRING);
echo json_encode($regular), "\n";
echo json_encode($text), "\n";
Output
{"c":"100","a":"10","b":"9"}
{"b":"9","c":"100","a":"10"}

Edge cases

  • Empty arrays are fine. Nothing changes and the function still returns true.
  • Integer keys are preserved too. arsort() does not renumber them, which is the main difference from rsort().
  • Mixed types under SORT_REGULAR follow PHP's general comparison rules, so mixing numbers, numeric strings and non-numeric strings can give results that are surprising. Pick an explicit flag when your data is not uniform.
  • Locale-based sorting with SORT_LOCALE_STRING depends on the environment, so avoid it when you need reproducible output.
  • Nested arrays are compared as arrays under SORT_REGULAR. If you need to sort by one field inside each element, use uasort() with a comparison callback.

Common mistakes

  1. Assigning the result. Writing $sorted = arsort($data); stores true, not an array. Call arsort($data); and then use $data.
  2. Passing a temporary. Because the parameter is by reference, arsort(getScores()); is not valid for a function result. Store the value in a variable first.
  3. Expecting a list. After sorting, the keys are not 0, 1, 2... in order. If you need a reindexed list, apply array_values() afterwards.
  4. Using the wrong function. arsort() sorts by value. If you want the keys ordered, use krsort(); if you want ascending values with keys kept, use asort().
  5. Ignoring the flag. Strings that look like numbers can sort numerically or alphabetically depending on $flags, so state your intent explicitly.
  • asort() sorts values ascending and keeps keys.
  • sort() and rsort() sort values ascending or descending and reindex the keys.
  • krsort() sorts by key in descending order.
  • uasort() sorts by value using your own comparison function and keeps keys.
  • array_multisort() sorts several arrays or dimensions at once.

Recap

Use arsort() when you want the biggest values first without losing the keys. It modifies the array in place, returns true, keeps equal elements in their original order, and accepts flags to control how values are compared. Remember to use the variable afterwards, not the return value.

Tested examples

Sort a leaderboard by score, highest first
<?php
$scores = ["ana" => 82, "ben" => 95, "cy" => 70];
arsort($scores);
echo json_encode($scores, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"ben":95,"ana":82,"cy":70}
Ties keep their original order and the return value is true
<?php
$votes = ["red" => 3, "green" => 5, "blue" => 3, "gold" => 5];
$result = arsort($votes);
var_dump($result);
echo json_encode($votes, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
bool(true)
{"green":5,"gold":5,"red":3,"blue":3}
Compare default sorting with SORT_STRING
<?php
$items = ["a" => "10", "b" => "9", "c" => "100"];
$regular = $items;
$text = $items;
arsort($regular);
arsort($text, SORT_STRING);
echo json_encode($regular), "\n";
echo json_encode($text), "\n";
Output
{"c":"100","a":"10","b":"9"}
{"b":"9","c":"100","a":"10"}