PHP 8 reference · Array Sorting Functions

PHP rsort() Function

Sort an array's values in descending order, in place. Keys are discarded and the result is renumbered from 0.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
array The array to sort (changed in place).
flags optional SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_NATURAL, optionally | SORT_FLAG_CASE. Default: SORT_REGULAR

Return value

Always true.

Examples

Highest firstTry it yourself
<?php
$scores = [72, 95, 88];
rsort($scores);
echo implode(", ", $scores), "\n";

Output

95, 88, 72
Natural order, descendingTry it yourself
<?php
$versions = ["1.10", "1.9", "1.2"];
rsort($versions, SORT_NATURAL);
echo implode(", ", $versions), "\n";

Output

1.10, 1.9, 1.2

Version history

VersionChange
PHP 4.0Added
PHP 8.0Sorting is stable: elements that compare equal keep their original order.
PHP 8.2The return type is true (it was bool).

In other languages

Was this page helpful?