PHP rsort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, rsort() sorts the values of an array from highest to lowest and gives them new keys 0, 1, 2 and so on.

The "r" stands for reverse. It is the descending version of sort(). The array is changed in place, and the old keys are thrown away. If you need to keep keys, use arsort() instead.

Syntax

Call rsort() with the values you need. Start with this line.

PHP
rsort($scores);
CallWhat it does
rsort($scores)Sorts the values from highest to lowest.
rsort($words, SORT_STRING)Compares the values as text.
sort($scores)The ascending version of the same sort.

rsort() changes the original array and returns true. All old keys are replaced by new numeric keys starting at 0.

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

Parameters

ParameterDescription
arrayRequired. The array to sort. It is passed by reference and changed in place.
flagsOptional. How to compare the values. SORT_REGULAR (default), SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL, and SORT_FLAG_CASE combined with SORT_STRING or SORT_NATURAL.

Return Value

Return ValueDescription
trueAlways returns true. The sorted result is stored in the array you passed in.

PHP Examples

Example 1 — Sort Numbers from High to Low

The largest number moves to the front of the array.

PHP
<?php
$scores = [3, 1, 2, 5];

rsort($scores);

echo implode(", ", $scores), PHP_EOL;

How It Works

  1. $scores holds four numbers in no particular order.
  2. rsort() sorts them from largest to smallest.
  3. The array itself is changed, so there is no need to save a return value.
  4. implode() prints the sorted numbers on one line.

Example 2 — Keys Are Replaced

rsort() discards the original keys. print_r() shows the new numeric keys.

PHP
<?php
$points = ["a" => 3, "b" => 9, "c" => 1];

rsort($points);

print_r($points);

How It Works

  1. The original array uses the string keys a, b and c.
  2. rsort() sorts the values from high to low.
  3. The old keys are removed, which is the biggest difference from arsort().
  4. The new keys are 0, 1 and 2, as print_r() shows.

Example 3 — Numeric Order Versus Text Order

Numbers stored as strings are compared as numbers by default. SORT_STRING compares them as text.

PHP
<?php
$a = ["10", "9", "2", "1"];
rsort($a);
echo implode(", ", $a), PHP_EOL;

rsort($a, SORT_STRING);
echo implode(", ", $a), PHP_EOL;

How It Works

  1. With the default flag, numeric strings are compared as numbers, so 10 is the largest.
  2. The first line shows the order 10, 9, 2, 1.
  3. SORT_STRING compares character by character, so "9" is greater than "2", then "10", then "1".
  4. The second line shows 9, 2, 10, 1.

Frequently Asked Questions

It sorts the values of an array in descending order and renumbers the keys from 0.
rsort() replaces the keys with 0, 1, 2. arsort() sorts by value in descending order and keeps the keys.
sort() sorts from low to high. rsort() sorts from high to low. Both renumber the keys.
No. It sorts the array you pass in and returns true.
Yes. Strings are sorted by their character codes by default, so capital letters sort after lowercase letters in descending order. Use SORT_FLAG_CASE | SORT_STRING to ignore the case.

Next: shuffle()

shuffle() puts the elements of an array in a random order and gives them new keys 0, 1, 2 .

shuffle() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful