How to use rsort() Function In PHP

The rsort() function sorts an array in descending order. Like sort(), it does not maintain the original keys.

Example: This example shows the implementation of the above-explained approach.

PHP
<?php

// Create an Array
$arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];

// Function to sort array in
// descending order
rsort($arr);

print_r($arr);

?>

Output
Array
(
    [0] => 9
    [1] => 6
    [2] => 5
    [3] => 5
    [4] => 5
    [5] => 4
    [6] => 3
    [7] => 3
    [8] => 2
    [9] => 1
    [10] => 1
)

How to Sort Numeric Array in PHP?

Given a numeric array, the task is to sort the numeric array using PHP. There are various built-in functions to sort arrays in different ways.

Below are the approaches to sort numeric arrays in PHP:

Table of Content

  • Using sort() Function
  • Using rsort() Function
  • Using asort() Function
  • Using arsort() Function
  • Using usort() Function

Similar Reads

Using sort() Function

The sort() function sorts an array in ascending order. It sorts the values without maintaining the original keys....

Using rsort() Function

The rsort() function sorts an array in descending order. Like sort(), it does not maintain the original keys....

Using asort() Function

The asort() function sorts an array in ascending order while maintaining the original keys....

Using arsort() Function

The arsort() function sorts an array in descending order while maintaining the original keys....

Using usort() Function

The usort() function sorts an array by values using a user-defined comparison function. This is useful when you need a custom sorting logic....

Contact Us