PHP rsort function tutorial shows how to sort arrays in descending order in PHP. Learn rsort with practical examples.
last modified March 13, 2025
The PHP rsort function sorts an array in descending order. It modifies the original array and returns a boolean indicating success.
The rsort function sorts an array by values in reverse order. It maintains index association for associative arrays but resets numeric keys.
Syntax: rsort(array &$array, int $flags = SORT_REGULAR): bool. The function returns true on success, false on failure. The array is passed by reference.
This demonstrates sorting a simple numeric array in descending order.
basic_rsort.php
<?php
$numbers = [3, 1, 4, 1, 5, 9, 2, 6]; rsort($numbers);
print_r($numbers);
The array is sorted from highest to lowest value. The original array is modified, and numeric keys are reindexed starting from 0.
The rsort function can also sort string arrays alphabetically in reverse order.
string_rsort.php
<?php
$fruits = [“apple”, “Orange”, “banana”, “cherry”]; rsort($fruits);
print_r($fruits);
Strings are sorted in reverse alphabetical order. Note that uppercase letters come before lowercase in ASCII order, affecting the sort result.
The second parameter allows specifying sorting behavior with various flags.
rsort_flags.php
<?php
$mixed = [“10”, 2, “1”, 20]; rsort($mixed, SORT_NUMERIC);
print_r($mixed);
Using SORT_NUMERIC treats values as numbers during comparison. String numbers are converted to numeric values for proper numerical sorting.
With associative arrays, rsort maintains value-key association but resets numeric keys.
assoc_rsort.php
<?php
$prices = [ “apple” => 1.2, “banana” => 0.5, “orange” => 0.8 ]; rsort($prices);
print_r($prices);
The values are sorted in descending order, but string keys are lost. Only the values remain with new numeric indices.
Combine rsort with strcasecmp for case-insensitive sorting of string arrays.
case_insensitive_rsort.php
<?php
$words = [“Apple”, “banana”, “cherry”, “apricot”]; rsort($words, SORT_STRING | SORT_FLAG_CASE);
print_r($words);
The SORT_FLAG_CASE flag makes the sorting case-insensitive. “Apple” and “apricot” are properly ordered despite different cases.
Preserve Keys: Use arsort to keep key-value associations.
Memory Usage: Be aware rsort modifies the original array.
Custom Sorting: For complex sorts, consider usort.
Large Arrays: Test performance with very large datasets.
This tutorial covered the PHP rsort function with practical examples showing its usage for sorting arrays in descending order.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP Array Functions.