PHP Array Sort

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 variety 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 list of in-built functions to perform these type of sorting functionalities based on various conditions required. These are,

  1. sort() – sorts an array in ascending order; it has two arguments that is 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.
  2. rsort() – This functions accepts same set of argument as similar as sort(), but sorting will be done in reverse order.
  3. 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.
  4. arsort() – It preserves the key value correlation after performing sort operation in reverse order.
  5. 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 the integer values returned by this function, the sorting will be done.
  6. 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 user.
  7. array_multisort()  – This function is used to perform sort over multiple array at a time. It also used to sort multi dimensional array by specifying sort options.
  8. natsort() – The above PHP sorting functions listed here will order the elements of an array based on standard way of ordering. But natsort(), is used to perform sorting as like as how a person can sort it manually.
  9. natcasesort() – This is similar to natsort()  but being used in case insensitive manner.

php_array_sort

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,

  1. ksort() – It is as like as sort() which will sort array elements in ascending order, but, based on key instead.
  2. krsort() – This function is used to sort an array based on its key but in reverse order.
  3. 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.

  1. 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.
  2. SORT_NUMERIC – As per the name of this option, it will allow sorting numerically.
  3. SORT_STRING – By specifying this flag option, an will be sorted in alphabetical order.
  4. SORT_NATURAL – This will provide manual sorting result.
  5. SORT_FLAG_CASE – If this flag is on, then the alphabetical ordering will be done in case insensitive manner.
  6. SORT_LOCALE_STRING – This flag will allow sorting alphabetically based on the language configured or specified by setlocale() 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");
sort($rollNumbers,SORT_STRING);
echo "sort() results<br/>";
print "<PRE>";
print_r($rollNumbers);
print "</PRE>";

In this program the sort() function is used to order the roll numbers array in standard format. By specifying the sort option as SORT_STRING, all the elements will be sorted alphabetically. And the output is,

Array
(
    [0] => 001
    [1] => 018
    [2] => 04
    [3] => 101
    [4] => 111
)

In other 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,

natsort($rollNumbers);

Now the result will be like how a person can sort this roll number array as shown below.

Array
(
    [4] => 001
    [2] => 04
    [0] => 018
    [1] => 101
    [3] => 111
)

This PHP code tutorial was published on June 16, 2013.

↑ Back to Top