Google News
logo
PHP Program to rsort() - Sort array in descending numerical order
In the following example of PHP program that demonstrates how to use the `rsort()` function to sort an array of numbers in descending numerical order :
Program :
<?php

// Create an array of numbers
$numbers = array(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5);

// Sort the array in descending numerical order
rsort($numbers);

// Loop through the sorted array and print the values
foreach($numbers as $number) {
    echo $number . "<br>";
}

?>
Output :
9
6
5
5
5
4
3
3
2
1
1
In this example, we have created an array of numbers `$numbers`. We use the `rsort()` function to sort the array in descending numerical order.

The sorted array is then looped through using a `foreach` loop and the values of each element are printed to the screen.

The `rsort()` function works by comparing the values of the elements in the array and swapping them if necessary to put them in descending order. By default, it uses a comparison based on the values of the elements themselves.

If you want to sort the array using a custom comparison function, you can use the `usort()` function instead.