PHP Functions For Sorting Array In Descending Order: rsort().
The rsort() function sorts an indexed array in descending order.
Sort the elements of the $MyArray array in descending numerical order:
<?php $MyArray=array(8, 20, 4, 100, 50, 6, 9, 11, 22, 5); rsort($MyArray); print_r($MyArray); ?>
Output:
Array ( [0] => 100 [1] => 50 [2] => 22 [3] => 20 [4] => 11 [5] => 9 [6] => 8 [7] => 6 [8] => 5 [9] => 4 )
Sort the elements of the $MyArray array in descending alphabetical order:
<?php $MyArray=array("PHP", "HTML", "CSS", "JavaScript", "Mysql", "Java", "AngularJs", "WordPress"); rsort($MyArray); print_r($MyArray); ?>
Output:
Array ( [0] => WordPress [1] => PHP [2] => Mysql [3] => JavaScript [4] => Java [5] => HTML [6] => CSS [7] => AngularJs )
Sorting using krsort() – Sort an array by key in reverse order
<?php $MyArray=array("PHP", "HTML", "CSS", "JavaScript", "Mysql", "Java", "AngularJs", "WordPress"); krsort($MyArray); print_r($MyArray); ?>
Output:
Array ( [7] => WordPress [6] => AngularJs [5] => Java [4] => Mysql [3] => JavaScript [2] => CSS [1] => HTML [0] => PHP )
My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.