w3resource

PHP Array Exercises : Sort an array in reverse order

PHP Array: Exercise-28 with Solution

Write a PHP script to sort an array in reverse order (highest to lowest).

Sample Solution:

PHP Code:

<?php
// Define an array of colors
$colors = array("Red", "Orange", "Black", "White");

// Sort the array in reverse order (descending)
rsort($colors);

// Display the sorted array
print_r($colors);
?>

Output:

Array                                                       
(                                                           
    [0] => White                                            
    [1] => Red                                              
    [2] => Orange                                           
    [3] => Black                                            
)

Flowchart:

Flowchart: PHP - Sort an array in reverse order

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP function to generate a random password (contains uppercase, lowercase, numeric and other) using shuffle() function.
Next: Write a PHP program to generate an array with a range taken from a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.