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:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/php-exercises/php-array-exercise-28.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics