Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

rsort

Sorts an array in descending order.

Syntax

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

Parameters

ParameterTypeDescription
$arrayarrayThe array being sorted.
$flagsintOptional. The comparison mode.

Return value

trueAlways `true`; the array itself is modified.

Examples

From largest to smallest:

<?php
 
$numbers = [3, 10, 1];
 
rsort($numbers);
 
echo implode(', ', $numbers);

Output:

10, 3, 1

The top three scores:

<?php
 
$scores = [70, 95, 82, 60, 91];
 
rsort($scores);
 
echo implode(', ', array_slice($scores, 0, 3));

Output:

95, 91, 82

Notes

The keys are replaced with numbers from zero.

See also

  • sort — Sorts an array in ascending order.
  • usort — Sorts an array by a custom comparison rule.

Related recipes

In the tutorial

  • Sorting arrays — Sorting in PHP — sort, rsort, asort, ksort, a custom rule with usort and the spaceship operator.

Thanks for reading!

Donate to the site — we work hard for you.