In this article, you will learn how to sort an indexed array in descending order. The rsort() function arranges the elements of an indexed array in descending order.
What is the syntax of the rsort() function in PHP?
rsort(array, sorttype)
Parameter | Details |
---|---|
array | The array to sort in descending order – Required |
sorttype | Optional. Specifies how to compare the array elements/items. SORT_STRING (default) – Compare elements as strings SORT_REGULAR – Compares normally without changing the type SORT_NUMERIC – Compare as numeric values SORT_LOCALE_STRING – Compare as a string according to the current locale |
Examples of rsort() function
Example 1. Sort the elements of the array in descending order alphabetically.
<?php
$arr=array("Suzuki","Corolla","Honda");
rsort($arr);
?>
Example 2. Sort the elements of the array in descending order numerically.
<?php
$n=array(5,7,4,6,23,32);
rsort($n);
?>