Array sorting in PHP

A
Arrays in PHP
Request Method in PHP and Super Globals in PHP

In the previous tutorial on PHP arrays we learned about declaring and iterating indexed, associative and multi-dimensional arrays. In this article we will dig deeper into PHP array sorting. Furthermore we will discuss array related functions that will make working with arrays a lot more easy.

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

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. When successful it will return true. In all other cases it will return false.

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

rSort(array, sort_type)

The rsort function 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);
?>

asort(array, sort_type)

The 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 pairs it returns the same array.

<?php
$age=array("apple"=>"1","pineapple"=>"2","orange"=>"3");
asort($age);
?>

ksort(array, sort_type)

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

<?php
$age=array("apple"=>"1","pineapple"=>"2","orange"=>"3");
ksort($age);
?>

arsort(array, sort_type)

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

<?php
$age=array("apple"=>"1","pineapple"=>"2","grapefruit"=>"3");
asort($age);
?>

krsort(array, sort_type)

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

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

The second parameter in the sorting functions is sort_type. By using one of these options you can manipulate the outcome.

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 the usort, uasort and uksort functions in PHP.

usort(array, my_sorting_function)

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

<?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)

This sorts an array (or associative array) according to the user defined function. The uasort function performs sorting according to the value of the associative array. For the keys we have the uksort function.

<?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)

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

<?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");
?>

array functions in PHP

Array_sum

This function returns the sum of the array values. It accepts both the indexed array as the associative array. In the case of an associative array it returns the sum of the values.

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

array_splice (array, offset, length, replacement)

The array_splice function removes elements from the array and replaces them with new elements.

Note: remember that the array_splice function does not preserve the keys in the returned value.

<?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(value, array, strict)

The array_search function searches an array for the specified element. If the element is found, it returns the key.

The strict parameter can be true or false. If true the search value must be of the same data type. The string “5” and number 5 are not the same in case strict equals true. If strict is set to false array_search will consider “5” and 5 as the same.

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

array_reverse (array, preserve_keys)

The array_reverse function returns the elements of an array in the reverse order. If the second parameter is set to true the keys in the output are preserved.

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

array_push(array, value1, value2, …)

This function pushes elements into the array. Each value is separated by a comma and the value can also be an array.

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

Note: if you only want to add one element it is better and easier to use $array[].

array_pop()

This function pops the element off the end of the array, shortening the array by one element.

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

For PHP 8 official PHP array sorting reference, click here.

Arrays in PHP
Request Method in PHP and Super Globals in PHP

Tutor Network

Share this

Learn PHP from A to Z