Home >>PHP Array Functions >PHP rsort() Function

PHP rsort() Function

PHP rsort() Function

PHP rsort() function is used to sort the given input array in descending order. It sorts the actual array and changes are reflected in the array itself.  It returns a boolean value TRUE on success and False in failure.

Syntax:

rsort($array, $sorttype);

Parameter Values

Parameter Description
array This is a required parameter. It defines the array to sort.
sorttype This is an optional parameter. It defines how to compare the array elements.

Here is an example of rsort() function in PHP:

<html>
<body>
<pre>
<?php
$x=array("A","B","C","D","E","F","G","H","I","J","K","L");
rsort($x);
print_r($x);
?>
</pre>
</body>
</html>
Output:
Array
(
    [0] => L
    [1] => K
    [2] => J
    [3] => I
    [4] => H
    [5] => G
    [6] => F
    [7] => E
    [8] => D
    [9] => C
    [10] => B
    [11] => A
)

Example 2:

<html>
<body>
<pre>
<?php
$x=array("61","23","15","19","39","28","82","58","93","29","27","44");
rsort($x);
print_r($x);
?>
</pre>
</body>
</html>
Output:
Array
(
    [0] => 93
    [1] => 82
    [2] => 61
    [3] => 58
    [4] => 44
    [5] => 39
    [6] => 29
    [7] => 28
    [8] => 27
    [9] => 23
    [10] => 19
    [11] => 15
)

No Sidebar ads