PHP Function Rsort - Sorting Arrays in Decadent Order
A variable used to store a set of data elements identified by a unique key or index (which can be either a string or a number). Arrays are a powerful feature in PHP and offer developers an easy way to arrange numerous values in an organized manner.
Sorting arrays is a common task that developers have to perform. It is a very important process as it helps to retrieve the desired values of an array in a quick and easy manner. PHP offers several functions for sorting arrays, like sort(), asort(), ksort() and usort(). The function sort() arranges the items in an associative array in ascending order based on their values, while preserving the association between keys and values.
In this article we will discuss about a very useful php function rsort which is similar to the sort() function but sorts an array in descending order (highest to lowest).
The rsort() function works on the actual array so changes are reflected directly in the array itself. The only parameter required is the array to sort and the optional sort_type argument decides how to compare the items. The sort_type can be SORT_REGULAR, SORT_NUMERIC or SORT_STRING.
In this example, we have an array with the values of Lemon, Apple and Grape. The rsort() function will sort this array in descending order and return the result. To try this function out for yourself, download the source code below. This is a very simple script and will not require any installation or configurations.