PHP asort is an inbuilt Function in PHP. It sorts arrays in ascending order while maintaining the relationship between keys and values. The smallest element in the input array appears first and the largest appears last. Also, it provides six sorting types to change the sorting behavior. In this article, we will discuss the PHP asort Function. Also, we will discuss a few examples of using it.
In order to sort an array in descending order, you can use PHP arsort Function.
Note: This function is mostly helpful in sorting Associative Arrays because the because it preserves the relation between keys and values.
bool asort( $array, $sortingType = SORT_REGULAR )
The PHP asort 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 asort 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 asort Function in PHP.
Example 1: Sort Integer Array in Ascending Order
First, let’s sort an array using the PHP sort Function.
<?php $array = array(5, 1, 2, 7, 3); sort($array); print_r($array); //Keys of the original array not preserved. /* Array (  => 1  => 2  => 3  => 5  => 7 ) */ ?>
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 asort Function.
<?php $array = array(5, 1, 2, 7, 3); asort($array); print_r($array); //Keys are preserved /* Array (  => 1  => 2  => 3  => 5  => 7 ) */ ?>
You can observe that the function preserves the original keys of the input array.
Example 2: Sort Strings in Ascending Order
Since, the PHP asort 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'); asort($array); print_r($array); /* Array ( [a] => apple [b] => banana [c] => cat ) */ ?>
In conclusion, we discussed the PHP asort 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 an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.