Arrays7 min readExamples run on PHP 8.5.10

rsort()

Learn how PHP 8.4 rsort() sorts array values from highest to lowest in place, reindexes keys, accepts sorting flags and returns true.

Signature

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

Purpose

rsort() reorders the values of an array from highest to lowest. It works directly on the variable you pass, so it changes the original array instead of returning a sorted copy. Use it when you only care about the values, for example a leaderboard of scores or a list of version numbers, and you do not need to keep the existing keys.

Signature

Parameters and defaults

  • $array is taken by reference. You must pass a variable (or another writable expression), not a literal array or the result of a function call.
  • $flags controls how values are compared. The default is SORT_REGULAR, which uses PHP's normal comparison rules.

The flags documented for rsort() are:

  • SORT_REGULAR compares items normally.
  • SORT_NUMERIC compares items numerically.
  • 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, as natsort() does.
  • SORT_FLAG_CASE can be combined with SORT_STRING or SORT_NATURAL using bitwise OR to ignore case.

Return value and failure cases

Since PHP 8.2 the declared return type is true; before that it was bool. The function always returns true, so checking the result tells you nothing. Real mistakes show up differently: passing something that cannot be taken by reference causes an error, and passing a non-array value is a type error.

Example 1: basic numeric sort

The highest value comes first, and duplicates are kept.

Example
<?php
$scores = [72, 95, 88, 95, 60];
rsort($scores);
echo json_encode($scores), "\n";
Output
[95,95,88,72,60]

Example 2: keys are replaced

rsort() assigns new sequential keys. The original string keys are discarded, not merely reordered. The function also returns true.

Example
<?php
$stock = ['b' => 5, 'a' => 12, 'c' => 8];
$result = rsort($stock);
var_dump($result);
echo json_encode($stock), "\n";
Output
bool(true)
[12,8,5]

If the keys matter, use arsort() to sort by value descending while keeping the key association, or krsort() to sort by key.

Example 3: flags change the comparison

Numeric strings are compared as numbers under SORT_REGULAR, but as text under SORT_STRING. That produces different orders for the same data.

Example
<?php
$values = ["10", "9", "2", "1"];
$regular = $values;
$string = $values;
rsort($regular, SORT_REGULAR);
rsort($string, SORT_STRING);
echo json_encode($regular), "\n";
echo json_encode($string), "\n";
Output
["10","9","2","1"]
["9","2","10","1"]

With SORT_STRING, "9" is greater than "2", which is greater than "10", because strings are compared character by character from the left.

Edge cases

  • An empty array stays empty, and the function still returns true.
  • Elements that compare as equal keep their original relative order. This is guaranteed from PHP 8.0.0; earlier versions left it undefined.
  • The array's internal pointer is reset to the first element.
  • Mixed value types (numbers, numeric strings, plain strings) follow the comparison rules for the chosen flag, so results can be surprising under SORT_REGULAR. When your data is homogeneous, pick the matching flag explicitly.
  • SORT_FLAG_CASE only has an effect when combined with SORT_STRING or SORT_NATURAL.

Common mistakes

  1. Assigning the result. Writing $sorted = rsort($items); stores true, not an array. Call rsort($items); and then use $items.
  2. Expecting keys to survive. Associative keys are lost. Choose arsort() when you need them.
  3. Sorting a temporary value. Passing the return value of a function directly fails because the argument must be a variable. Assign it first.
  4. Ignoring the flag. Numeric strings such as "10" and "9" can sort differently depending on SORT_STRING versus numeric comparison. Set the flag when the type of comparison matters.
  5. Using it for custom ordering. rsort() has no comparison callback. For sorting by a field or a custom rule, use a user-defined comparison function with a function such as usort().
  • sort() sorts values in ascending order.
  • arsort() sorts values in descending order and keeps keys.
  • krsort() sorts by key in descending order.

Recap

rsort() sorts an array's values from highest to lowest, in place, and replaces the keys with a fresh 0-based list. It takes an optional flag to control comparison, resets the internal pointer, keeps equal elements in their original order, and always returns true. Use the flag that fits your data, and do not rely on the return value.

Tested examples

Sort numbers from highest to lowest
<?php
$scores = [72, 95, 88, 95, 60];
rsort($scores);
echo json_encode($scores), "\n";
Output
[95,95,88,72,60]
Keys are replaced and true is returned
<?php
$stock = ['b' => 5, 'a' => 12, 'c' => 8];
$result = rsort($stock);
var_dump($result);
echo json_encode($stock), "\n";
Output
bool(true)
[12,8,5]
Regular versus string comparison flags
<?php
$values = ["10", "9", "2", "1"];
$regular = $values;
$string = $values;
rsort($regular, SORT_REGULAR);
rsort($string, SORT_STRING);
echo json_encode($regular), "\n";
echo json_encode($string), "\n";
Output
["10","9","2","1"]
["9","2","10","1"]