PHP Array Sort
Last modified on February 25th, 2017 by Vincy.
PHP includes powerful array functions which are quite interesting and very useful for working with. For example, array_keys(), array_values, implode() and explode() and etc. On this list, sorting functions are one of the important set of functions to be known.
Sorting an array that includes several varieties of conditions based on which the group of elements of the array will be ordered. Based on these conditions the sorting is classified as many types, for example, key based sorting, value based sorting, natural ordering and etc.
PHP provides a list of inbuilt functions to perform these type of sorting functionalities based on various conditions required. These are,
- sort() – sorts an array in ascending order; it has two arguments that are the array to be sorted and the sorting options. This second argument is optional, if nothing is specified, then the default value will be SORT_REGULAR. Let us see, all possible values of these sort option argument in detail, later in this article.
- rsort() – This function accepts the same set of argument as similar as sort(), but sorting will be done in reverse order.
- asort() – It is used to sort the elements of an array in ascending order like sort() function. But, it preserves the key indices as it is after sorting.
- arsort() – It preserves the key value correlation after performing sort operation in reverse order.
- usort() – Instead of sort options, usort() accepts a callback functions name which will be called for each element of the array and apply some condition. Based on the integer values returned by this function, the sorting will be done.
- uasort() – It is the combination of usort() and asort() by preserving array key indices as it is after sorting with the call back function defined by the user.
- array_multisort() – This function is used to perform sort over multiple arrays at a time. It also used to sort the multi-dimensional array by specifying sort options.
- natsort() – The above PHP sorting functions listed here will order the elements of an array based on the standard way of ordering. But natsort(), is used to perform sorting as like as how a person can sort it manually.
- natcasesort() – This is similar to natsort() but being used in a case insensitive manner.
The classifications of PHP sorting functions listed above are used to order PHP array based or the values. Some more functions are there in PHP to sort an array based on keys. These are,
- ksort() – It is as like as sort() which will sort array elements in ascending order, but, based on key instead.
- krsort() – This function is used to sort an array based on its key but in reverse order.
- uksort() – Combination of ksort() and usort() to perform key based sorting with user defined call back function specified while invoking this method.
So, these are the PHP array sort functions to order the elements with several possible conditions. Now we need to know about the sort flag option values and the behavior of sorting based on these values.
- SORT_REGULAR – This is the default option if nothing is specified for the above sorting functions. For this option, the data type of array element will be taken as it is for sorting without any modification.
- SORT_NUMERIC – As per the name of this option, it will allow sorting numerically.
- SORT_STRING – By specifying this flag option, a will be sorted in alphabetical order.
- SORT_NATURAL – This will provide a manual sorting result.
- SORT_FLAG_CASE – If this flag is on, then the alphabetical ordering will be done in a case insensitive manner.
- SORT_LOCALE_STRING – This flag will allow sorting alphabetically based on the language configured or specified by set locale() function currently.
PHP Array Sort: Example
Let us see an example program that shows the difference between standard sorting and natural sorting using PHP sort() and natsort() respectively.
$rollNumbers = array("018", "101", "04", "111","001");
echo "sort() results<br/>";
In this program, the sort() function is used to order the roll numbers array in a standard format. By specifying the sort option as SORT_STRING, all the elements will be sorted alphabetically. And the output is,
 => 001
 => 018
 => 04
 => 101
 => 111
In another case, let us invoke PHP function natsort() for getting sort results based on natural ordering. For that, replace the line that is invoking sort() as,
Now the result will be like how a person can sort this roll number array as shown below.
 => 001
 => 04
 => 018
 => 101
 => 111
This PHP code tutorial was published on June 16, 2013.
↑ Back to Top