Content · Programming · PHP

PHP rsort() Function: Usage and Examples

Learn rsort() with clear PHP examples, output, practical scenarios, important behaviors, and common beginner mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP developers sometimes need to sort array values in descending order and reindex numeric keys. This comes up when preparing data, displaying results, or integrating information with other services.

The rsort() operation handles this particular task. The following examples show its result, its limitations, and a practical way to apply it.

What Is rsort() in PHP?

Sort array values in descending order and reindex numeric keys.

Its syntax is:

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

The syntax shows the expected parameters and return type. Arguments marked with an ampersand are passed by reference and may be modified. Optional parameters control additional behavior; values must match the declared types.

Important: It changes the input and reindexes keys. Do not use it when associated keys carry meaning.

Parameters:

  • &$array — May be updated by the function.
  • $flags — A named input or configuration argument; use the declared type and optional default shown above.

Example 1: Basic Usage

This first example uses a small input to demonstrate the main result of rsort().

<?php

$scores = [8, 21, 13]; rsort($scores); print_r($scores);

Output:

Array
(
    [0] => 21
    [1] => 13
    [2] => 8
)

The output demonstrates the primary operation: sort array values in descending order and reindex numeric keys.

Example 2: Understanding Another Case

This second example changes the input or option to reveal a different behavior of rsort().

<?php

$names = ["Zoe", "Ana", "Maya"]; rsort($names, SORT_STRING); print_r($names);

Output:

Array
(
    [0] => Zoe
    [1] => Maya
    [2] => Ana
)

Compared with the first example, this one illustrates the same operation with a different input shape or option. It changes the input and reindexes keys. Do not use it when associated keys carry meaning.

Practical Example: A Real Application Scenario

Here is a practical use of rsort() with realistic sample information. Adapt the values to match your application.

<?php

$priorityScores = [15, 90, 42]; rsort($priorityScores, SORT_NUMERIC); echo implode(", ", $priorityScores);

Output:

90, 42, 15

This is a useful starting point, but production code should check errors and validate values before performing the operation. It changes the input and reindexes keys. Do not use it when associated keys carry meaning.

Important Behavior and Edge Cases

It changes the input and reindexes keys. Do not use it when associated keys carry meaning.

  • Input: Use the expected PHP types and validate user-provided values.
  • Result: Check the return value separately from any modifications to the input.
  • Environment: For extension, database, or filesystem examples, verify availability and handle failures before accessing results.

Comparison With Related Functions

Function or ApproachWhen to Use It
rsort()Sort array values in descending order and reindex numeric keys.
Related functionsarsort() preserves associations while sorting values descending; sort() sorts ascending.

Common Beginner Mistakes

MistakeCorrect Approach
Expecting a sorted copy without changing the inputThese sort functions modify the array directly; copy it first if you need the original.
Using the wrong sort targetDecide whether to sort keys or values, and whether key associations must remain intact.
Assuming every comparison handles text in the same wayarsort() preserves associations while sorting values descending; sort() sorts ascending.

Key Takeaways

  • rsort() lets you sort array values in descending order and reindex numeric keys.
  • It changes the input and reindexes keys. Do not use it when associated keys carry meaning.
  • arsort() preserves associations while sorting values descending; sort() sorts ascending.
  • Check input, return values, and environmental requirements when adapting the examples.

rsort() provides a focused PHP operation for sort array values in descending order and reindex numeric keys. Start with the simple examples, then adapt the practical example with the correct checks for your project.

Share this resourceSend this page to someone who may find it useful.