In the previous tutorial on PHP Arrays. We have learned about declaring and iterating indexed, associative, and multi-dimensional arrays. We are now in a position to dig deeper into PHP Array Sorting. Besides this, we will also work with useful functions that really make working with arrays super easy.

Array Sorting functions in PHP

In PHP 8, we have the following functions to sort an array.

Function NameSort by order
sort() Ascending order
rsort()Descending order
asort()Associative arrays in ascending order, according to the value
ksort()Associative arrays in ascending order, according to the key
arsort()Descending order, according to the value
krsort()Descending order, according to the key
Table of array sorting functions in PHP

1. Sort(array, sort_type)

Sort function arranges the elements of an array in ascending order (lower to higher). If the array contains only one element, it will return the same array. It returns true in case of success and false in case of failure.

For instance

<?php
$num = array(4, 6, 2, 12, 11);
sort($num);
?>

2. rSort(array, sort_type)

rSort is the reverse of the sort function. It arranges the elements of an array in descending form (higher to lower). If the array contains only one element, it returns the same array.

<?php
$num=array(4,16,12,20,11);
rsort($num);
?>

3. asort(array, sort_type)

asort function arranges the elements of an associative array in ascending order according to the value of keys. If the array consists of single key and value pair, it returns the same array.

For instance

<?php
$age=array("Jawad"=>"23","Abdullah"=>"14","Adil"=>"19");
asort($age);
?>

4. ksort(array, sort_type)

ksort function also arranges the elements of an associative array in ascending order but according to the keys. If the array consists of single key and value pair, it returns the same array.

For instance

<?php
$age=array("Jawad"=>"23","Abdullah"=>"14","Adil"=>"19");
ksort($age);
?>

The second parameter in the sorting functions is sort_type. Following options are available. Options decide how to compare the elements of the array.

5. arsort(array, sort_type)

asort function arranges the elements of an associative array in descending order according to the values. If the array consists of single key and value pair, it returns the same array.

For instance

<?php
$age=array("Jawad"=>"23","Abdullah"=>"14","Adil"=>"19");
asort($age);
?>

6. krsort(array, sort_type)

krsort function also arranges the elements of an associative array in descending order but according to the keys. If the array consists of single key and value pair, it returns the same array.

For instance

<?php
$age=array("Jawad"=>"23","Abdullah"=>"14","Adil"=>"19");
ksort($age);
?>

The second parameter in the sorting functions is sort_type. Following options are available. Options decide how to compare the elements of the array.

Sorting typeDescription
0 = SORT_REGULARDefault. Compare items normally (don’t change types)
1 = SORT_NUMERIC Compare items numerically
2 = SORT_STRINGCompare items as strings
3 = SORT_LOCALE_STRINGCompare items as strings, based on current locale
4 = SORT_NATURALCompare items as strings using natural ordering
5 = SORT_FLAG_CASE
Array sorting options in sort and rsort function

Other than these basic and built-in array sorting functions, the user can also define its own sorting function in PHP. So, In this section, you will learn about usort, uasort and uksort function in PHP.

usort(array, my_sorting_function)

usort function accept two paramters. The first parameter is the array to sort and the second parameter is the user-defined function to sort.

For instance

<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}

$a=array(4,2,8,6);
usort($a,"my_sort");
?>

uasort(array, user_define_function)

Sort an array or associative array according to the user define function. The uasort function performs sorting according to the value of the associative array. For the keys, we have uksort function, which is given next.

For instance

<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}

$arr=array("a"=>4,"b"=>2,"c"=>8,"d"=>6);
uasort($arr,"my_sort");
?>

uksort(array, user_defined_function)

Sort an associative array using a user-defined function. uksort performs sorting according to keys of the associative array.

For instance

<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}

$arr=array("a"=>4,"b"=>2,"c"=>8,"d"=>6);
uksort($arr,"my_sort");
?>

Useful array functions in PHP

array_sum function returns the sum of array elements. This function accepts both indexed array and associative array. In the case of an associative array, it returns the sum of values.

For instance

<?php
$a=array(5,15,25);
echo array_sum($a);
?>

array_splice function removes specified elements from the array and replaces them with new elements. Remember that the array_splice function does not preserve the keys in the returned value.

For instance

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"purple","b"=>"orange");
print_r(array_splice($a1,0,2,$a2));
?>

array_search function simply searches an array for the element. If the element is found, it returns its key. This function accepts three parameters.

array_search(value, array, strict)

The third parameter strict can be either true or false. However, if it is true, the value to search in the array must be of the same data type. That is, the string “5” and number 5 are not the same in the case of strict = true. However, if strict is set to false, array_search will consider “5” and 5 as the same.

For instance

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
?>

array_reverse returns array having elements in the reverse order. If the second parameter is set to true, the keys are preserved in the output.

For instance

<?php
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));
?>

array_push(array, value1, value2, …) function push elements into the array. You can push multiple elements in an array by specifying them in the parameter of the function, starting from the second params. H Each value is separated by a comma and the value can also be an array.

For instance

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>

array_pop() removes the last element of the array.

For instance

<?php
$a=array("red","green","blue");
array_pop($a);
print_r($a);
?>

For PHP 8 official array sorting reference, click here.