5 Ways To Sort Arrays In PHP

Posted by TotalDC

Now that you know what an array is and how to create one, let’s look at 5 ways to sort arrays in PHP in ascending or descending order using built-in PHP functions.

Functions In PHP For Sorting Arrays

PHP has a number of built-in functions designed for sorting array elements in different ways. Let’s look at some of these functions most commonly used for sorting arrays.

  • sort() and rsort() — For sorting indexed arrays
  • asort() and arsort() — For sorting associative arrays by value
  • ksort() and krsort() — For sorting associative arrays by key

Sorting Indexed Arrays In Ascending Order

The sort() function is used for sorting the elements of the indexed array in ascending order. Let’s take a look at an example.

<?php

$colors = array("Red", "Green", "Blue", "Yellow");
 

sort($colors);
var_dump($colors);
?>

The result of this code would look like this:

array(4) { [0]=> string(3) "Red" [1]=> string(5) "Green" [2]=> string(4) "Blue" [3]=> string(6) "Yellow" }

Also, you can sort the numeric elements of the array in ascending order.

<?php

$numbers = array(1, 2, 2.5, 4, 7, 10);
 

sort($numbers);
var_dump($numbers);
?>

The result of this code would look like this:

array(5) { [0]=> 1 [1]=> 2 [2]=> 2.5 [3]=> 4 [4] => 7 [5] => 10 }

Sorting Indexed Arrays in Descending Order

The rsort() function is used for sorting the elements of the indexed array in descending order. Let’s look at the example:

<?php
$colors = array("Red", "Green", "Blue", "Yellow");
 
rsort($colors);
var_dump($colors);
?>

The result would look like this:

array(3) { [0]=> Yellow [1]=> Red [2]=> Green [3]=> Blue }

Just like that, you can sort the numeric elements of the array also in descending order.

<?php
$numbers = array(1, 2, 2.5, 4, 7, 10);
 
rsort($numbers);
var_dump($numbers);
?>

Your result would look like this:

array(6) { [0]=> 10 [1]=> 7 [2]=> 4 [3]=> 2.5 [4] => 2 [5] => 1 } 

Sorting Associative Arrays In Ascending Order By Value

The arsort() function sorts the elements of an associative array in descending order according to the value. It works just like rsort() but keeps the association between keys and its values. Here is an example:

<?php

$age = array("Paul"=>30, "Matt"=>14, "John"=>45, "Bob"=>35);
 
arsort($age);
var_dump($age);
?>

Result would be:

array(4) { [John]=> 45 [Bob]=> 34 [Paul]=> 30 [Matt]=> 14 } 

Sorting Associative Arrays In Ascending Order By Key

The ksort() function sorts the elements of an associative array in ascending order by keys. It keeps the association between keys and their values while sorting. Just like asort().

<?php

$age = array("Paul"=>30, "Matt"=>14, "John"=>45, "Bob"=>35);

 
ksort($age);
var_dump($age);
?>

You will get this result:

array(4) { [Bob]=> 35 [John]=> 45 [Matt]=> 14 [Paul]=> 30 } 

Sorting Associative Arrays In Descending Order By Key

The krsort() function sorts the elements of an associative array in descending order by key. It keeps the association between keys and its values while sorting. Just like arsort() function.

<?php

$age = array("Paul"=>30, "Matt"=>14, "John"=>45, "Bob"=>35);

 
ksort($age);
var_dump($age);
?>

Your result would look like this:

array(4) { [Paul]=> 30 [Matt]=> 14 [John]=> 45 [Bob]=> 35 }