PHP arsort is an inbuilt Function in PHP. It sorts arrays in descending order while maintaining the relationship between keys and values. The smallest element in the input array appears last and the largest appears first. Also, it provides six sorting types to change the sorting behavior. In this article, we will discuss the PHP arsort Function. Also, we will discuss a few examples of using it.
In order to sort an array in ascending order, you can use PHP asort Function.
Note: This function is mostly helpful in sorting Associative Arrays because the because it preserves the relation between keys and values.
bool arsort( $array, $sortingType = SORT_REGULAR )
The PHP arsort Function expects two parameters. However, only the first parameter is mandatory.
- $array: The first parameter to the function is an array which you need to sort. Also, it is a mandatory parameter.
- $sortingType: The function provides 6 sorting types which are optional to pass. This parameter can take the following values:
- SORT_REGULAR: On passing SORT_REGULAR or 0, the function compares the elements normally without changing their type. Also, this is the default value to the parameter.
- SORT_NUMERIC: On passing SORT_NUMERIC or 1, the function compares the elements numerically.
- SORT_STRING: The function compares the elements string-wise on passing SORT_STRING or 2.
- SORT_LOCALE_STRING: The function compares the elements as a string based on current locale on passing SORT_LOCALE_STRING or 3. It uses the locale which can be changed by the
- SORT_NATURAL: On passing 4 or SORT_NATURAL, the function compares the elements as strings using natural ordering.
- SORT_FLAG_CASE: On passing 5 or SORT_FLAG_CASE, the function compares the elements as case-insensitive strings. Also, this parameter can be used | (bitwise) with SORT_NATURAL or SORT_STRING.
The PHP arsort Function returns true on success and false on failure. However, it modifies the original input array.
Note: The function preserves the keys of the original array after sorting. Also, if two values are equal, their relative order in the sorted array is undefined.
Let’s discuss a few examples of using the arsort Function in PHP.
Example 1: Sort Integer Array in Descending Order
First, let’s sort an array using the PHP rsort Function.
<?php $array = array(5, 1, 2, 7, 3); rsort($array); print_r($array); //Keys of the original array not preserved. /* Array (  => 7  => 5  => 3  => 2  => 1 ) */ ?>
You can observe, the function re-indexes the output array. It does not preserve the keys of the original array.
Now, let’s sort the same array using PHP arsort Function.
<?php $array = array(5, 1, 2, 7, 3); arsort($array); print_r($array); //Keys are preserved /* Array (  => 7  => 5  => 3  => 2  => 1 ) */ ?>
You can observe that the function preserves the original keys of the input array.
Example 2: Sort Strings in Descending Order
Since, the PHP arsort function preserves the keys of the original array, it is more useful in sorting associative arrays.
<?php $array = array('b' => 'banana', 'c' => 'cat', 'a' => 'apple'); arsort($array); print_r($array); /* Array ( [c] => cat [b] => banana [a] => apple ) */ ?>
In conclusion, we discussed the PHP arsort Function in the above article. You can read more about it on PHP Official Documentation. Additionally, you can learn about more PHP Array Functions on Concatly.
Vishesh is currently working as a Senior Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.