PHP Array Sort

by Vincy. Last modified on July 19th, 2022.

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 sets 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 sorting is classified into many types, for example, key-based sorting, value-based sorting, natural ordering and etc.

PHP provides a list of inbuilt functions to perform these types of sorting functionalities based on various conditions required. These are,

  1. 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 sorts of option arguments in detail, later in this article.
  2. rsort() – This function accepts the same set of arguments 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 the sort operations 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 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 the user.
  7. array_multisort()  – This function is used to perform a sort over multiple arrays at a time. It is also used to sort the 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 the 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 is being used in a case insensitive manner.

The classifications of PHP sorting functions listed above are used to order PHP array-based or 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, a will be sorted in alphabetical order.
  4. SORT_NATURAL – This will provide a manual sorting result.
  5. SORT_FLAG_CASE – If this flag is on, then the alphabetical ordering will be done in a case-insensitive manner.
  6. SORT_LOCALE_STRING – This flag will allow sorting alphabetically based on the language configured or specified by the 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.

<?php
$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 a 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 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,

<?php
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
)
Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page