Learn How to Sort Arrays in PHP using asort() Function
Learn how to use PHP's asort() function to sort arrays by value in ascending order while preserving key-value associations, with syntax and examples.
As a web developer, you may find yourself in situations where you need to manipulate arrays in your PHP code. One such operation is sorting the array. The built-in asort() function in PHP can be used to sort an array by its values in ascending order. In this article, we will explore how to use the asort() function, its syntax, and examples of how to use it in your code.
Syntax of asort()
The asort() function sorts an array by its values in ascending order, while maintaining key-value associations. The syntax of the function is as follows:
Syntax of asort function
asort($array, $sorting_type);The first argument $array is the array to be sorted. The second argument $sorting_type is optional and specifies the sorting type. Possible values include:
SORT_REGULAR- compare items normallySORT_NUMERIC- compare items numericallySORT_STRING- compare items as stringsSORT_LOCALE_STRING- compare items as strings based on current locale
If the $sorting_type parameter is not provided, SORT_REGULAR is used by default. Note that asort() modifies the original array in place and returns true on success or false on failure — it does not return a new sorted array, so don't write $sorted = asort($array) expecting the sorted data in $sorted.
When to use asort()
The key thing that sets asort() apart from plain sort() is that it keeps each value attached to its original key. Use asort() when the keys carry meaning you can't afford to lose — for example an associative array mapping names to scores, ids to prices, or usernames to last-login timestamps. If you sort that kind of data with sort(), PHP throws the keys away and re-indexes the array 0, 1, 2, …, breaking the association.
Reach for sort() instead only when the array is a plain list and the keys are just positions you don't care about.
Examples of asort()
Here are a few examples that illustrate how to use the asort() function in PHP:
- Sorting an array of strings using
asort():
Examples of asort()
- Sorting an array of integers using
asort():
Sorting an array of integers using asort()
- Sorting an associative array using
asort():
PHP Sorting an associative array using asort()
Common gotchas
- Use
SORT_NUMERICfor numeric strings. With the defaultSORT_REGULAR, values like"10"and"9"may be compared as strings, putting"10"before"9". PassSORT_NUMERICwhen your values are numbers (even if stored as strings) to guarantee a numeric ordering. - Sorting is not stable before PHP 8.0. In PHP 8.0 and later, equal elements keep their original relative order; in earlier versions their order was undefined.
- It returns a boolean, not the array. Because the sort happens in place, treat the original variable as the result.
Related sorting functions
PHP offers a family of array-sorting functions, each preserving (or discarding) keys differently and sorting by value or by key:
sort()— sort by value ascending, re-indexes keys.rsort()— sort by value descending, re-indexes keys.arsort()— likeasort()but descending, keeping key associations.ksort()/krsort()— sort by key ascending / descending.usort()/uasort()— sort with a custom comparison callback.
See the Sorting Arrays overview for a side-by-side comparison.
Conclusion
The asort() function is a useful tool for sorting arrays in PHP by their values in ascending order, while maintaining key-value associations. It shines whenever your keys carry meaning — sorting an associative array of names to scores, ids to prices, and the like — where plain sort() would discard those keys. By understanding its syntax, sorting types, and the related sort functions, you can pick the right tool for each ordering task and write cleaner PHP code.