How to sort an array in PHP

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

In this article, we’ll explore multiple functions to sort an array in PHP. As an overview, here are the functions that we are going to explore.

  • sort() – Sorts an array in ascending order.
  • rsort() – Sorts an array in descending order.
  • asort() – Sorts an associate array by value, in ascending order.
  • arsort() – Sorts an associative array by value, in descending order.
  • ksort() – Sorts an associate array by key, in ascending order.
  • krsort() – Sorts an associate array by key, in descending order.

We have a lot to see so let’s jump straight to the topic.

Option 1 – Sort an array in PHP using sort()

The first function in the list is the sort function. This function sorts an array in ascending order. In the examples, we have called the sort function on names and numbers array to see both alphabetic and numeric sort.

<?php
//Array containing names.
$names_array = array("Zeus","Tina","Robert","Benjamin","Stacie","Anna","Franklin");
 
//Sort names in ascending order.
sort($names_array);
 
 
//Print the name array.
print_r($names_array);
 
/*
OUTPUT
Array
(
    [0] => Anna    
    [1] => Benjamin
    [2] => Franklin
    [3] => Robert  
    [4] => Stacie  
    [5] => Tina    
    [6] => Zeus
)
*/
?>

Perfectly sorted. Let’s see numeric sort now.

<?php
//Array containing numbers.
$numbers_array = array(1000,100,10,0,-1,-10,-100,-1000);
 
//Sort numbers in ascending order.
sort($numbers_array);
 
 
//Print the name array.
print_r($numbers_array);
 
/*
OUTPUT
Array
(
    [0] => -1000
    [1] => -100
    [2] => -10
    [3] => -1
    [4] => 0
    [5] => 10
    [6] => 100
    [7] => 1000
)
*/
?>

All well. These examples were quite straightforward. Let’s see an anomalous example.

<?php
//Array containing names as keys and ages as values.
$versions_array = array("Version1","Version2","Version10","Version11");
 
//Sort the $versions_array
sort($versions_array);
 
 
//Print the name array.
print_r($versions_array);
 
/*
OUTPUT
Array
(
    [0] => Version1 
    [1] => Version10
    [2] => Version11
    [3] => Version2 
)
*/
?>

The output doesn’t make much sense, right? It is not how we would have sorted it. We will revisit this later in the article. We know we have your curiosity but let’s complete our list of functions before finding a workaround for this anomaly.

Option 2 – Sort an array in PHP using rsort()

The rsort function sorts an array in descending order. Let’s retry the examples with rsort.

<?php
//Array containing names.
$names_array = array("Zeus","Tina","Robert","Benjamin","Stacie","Anna","Franklin");
 
//Sort names in descending order.
rsort($names_array);
 
 
//Print the name array.
print_r($names_array);
 
/*
OUTPUT
Array
(
    [0] => Zeus    
    [1] => Tina    
    [2] => Stacie  
    [3] => Robert  
    [4] => Franklin
    [5] => Benjamin
    [6] => Anna
)
*/
?>

Nothing unexpected. The array has been arranged alphabetically in descending order.

<?php
//Array containing numbers.
$numbers_array = array(1000,100,10,0,-1,-10,-100,-1000);
 
//Sort numbers in descending order.
rsort($numbers_array);
 
 
//Print the name array.
print_r($numbers_array);
 
/*
OUTPUT
Array
(
    [0] => 1000
    [1] => 100 
    [2] => 10  
    [3] => 0   
    [4] => -1  
    [5] => -10 
    [6] => -100
    [7] => -1000
)
*/
?>

The same goes for the numbers array. So far, we’ve seen sort and rsort functions. Now, what if we’ve key-values pair in an array. We call such arrays associative. Next, we’ll see how to sort arrays in PHP that are associative.

Option 3 – Sort an array in PHP using asort()

Associative arrays have key-value pairs. So, we can sort it either by key or by value. PHP’s asort function sorts values in ascending order. The example shows an array with name and age key-value pairs. We’ll sort the array with asort.

<?php
//Array containing names as keys and ages as values.
$name_age_array = array("Zeus"=>34,"Tina"=>22,"Robert"=>44,"Benjamin"=>50,"Stacie"=>20,"Anna"=>25,"Franklin"=>24);
 
//Sort the $name_age_array by value in ascending order.
asort($name_age_array);
 
 
//Print the name array.
print_r($name_age_array);
 
/*
OUTPUT
Array
(
    [Stacie] => 20  
    [Tina] => 22    
    [Franklin] => 24
    [Anna] => 25    
    [Zeus] => 34    
    [Robert] => 44  
    [Benjamin] => 50
)
*/
?>

See, the array is sorted by age, or in other words by value. Next, we see a variant of asort.

Option 4 – Sort an array in PHP using arsort()

The function arsort sorts the array by value in descending order. That’s the only difference with asort. Let’s rerun the same code with arsort.

<?php
//Array containing names as keys and ages as values.
$name_age_array = array("Zeus"=>34,"Tina"=>22,"Robert"=>44,"Benjamin"=>50,"Stacie"=>20,"Anna"=>25,"Franklin"=>24);
 
//Sort the $name_age_array by value in descending order.
arsort($name_age_array);
 
 
//Print the name array.
print_r($name_age_array);
 
/*
OUTPUT
Array
(
    [Benjamin] => 50
    [Robert] => 44  
    [Zeus] => 34    
    [Anna] => 25    
    [Franklin] => 24
    [Tina] => 22    
    [Stacie] => 20
)
*/
?>

Works as expected. The values or ages are sorted in descending order, from high to low. We’ve just seen asort and arsort sorts value in a key-value pair. Next, we will see a couple of functions for sorting keys rather than values.

Option 5 – Sort an array in PHP using ksort()

The function ksort sorts an associate array by key values, in ascending order. Let’s see the name and age array example. We’ll sort it by key values, or names.

<?php
//Array containing names as keys and ages as values.
$name_age_array = array("Zeus"=>34,"Tina"=>22,"Robert"=>44,"Benjamin"=>50,"Stacie"=>20,"Anna"=>25,"Franklin"=>24);
 
//Sort the $name_age_array by key in ascending order.
ksort($name_age_array);
 
 
//Print the name array.
print_r($name_age_array);
 
/*
OUTPUT
Array
(
    [Anna] => 25    
    [Benjamin] => 50
    [Franklin] => 24
    [Robert] => 44  
    [Stacie] => 20  
    [Tina] => 22    
    [Zeus] => 34
)
*/
?>

The ksort function sorts the array keys alphabetically. No rocket science!

Option 6 – Sort an array in PHP using krsort()

Perhaps by now, you are used to this nomenclature. The krsort sorts the keys in descending order. So, we’ll redo the example with krsort.

<?php
//Array containing names as keys and ages as values.
$name_age_array = array("Zeus"=>34,"Tina"=>22,"Robert"=>44,"Benjamin"=>50,"Stacie"=>20,"Anna"=>25,"Franklin"=>24);
 
//Sort the $name_age_array by key in descending order.
krsort($name_age_array);
 
 
//Print the name array.
print_r($name_age_array);
 
/*
OUTPUT
Array
(
    [Zeus] => 34    
    [Tina] => 22    
    [Stacie] => 20  
    [Robert] => 44  
    [Franklin] => 24
    [Benjamin] => 50
    [Anna] => 25
)
*/
?>

Just what we have expected. Now is the time to revisit the anomaly we’d encountered earlier in this article.

The Anomaly – Sorted

Let’s revisit the example we encountered in the sort function that had an unexpected output. 

<?php
//Array containing names as keys and ages as values.
$versions_array = array("Version1","Version2","Version10","Version11");
 
//Sort the $versions_array
sort($versions_array);
 
 
//Print the name array.
print_r($versions_array);
 
/*
OUTPUT
Array
(
    [0] => Version1 
    [1] => Version10
    [2] => Version11
    [3] => Version2 
)
*/
?>

PHP calls this standard sorting. Infact, there’s a second argument in the sort function that decides the mode of sorting. By default, the sort function resorts to SORT_REGULAR, or standard sorting in other terms. We won’t discuss these modes in detail here. However, you can check out documentation for clarity.

We’ll explore two options to sort this array the way we would do naturally. First, by changing sort function’s mode and then by using natsort function.

#1 – SORT_NATURAL

We will pass SORT_NATURAL as second argument to sort function. This would change the default mode to natural sort. The natural sort algorithm then sorts the array just the way a human would do.

<?php
//Array containing names as keys and ages as values.
$versions_array = array("Version1","Version2","Version10","Version11");
 
//Sort the $versions_array (Natural Sort)
sort($versions_array,SORT_NATURAL);
 
 
//Print the name array.
print_r($versions_array);
 
/*
OUTPUT
Array
(
    [0] => Version1 
    [1] => Version2 
    [2] => Version10
    [3] => Version11
)
*/

 ?>

#2 – natsort function

The natsort function sorts an array using natural order algorithm. Let’s use this function to see the output.

<?php
//Array containing names as keys and ages as values.
$versions_array = array("Version1","Version2","Version10","Version11");
 
//Sort the $versions_array (Natural order algorithm)
natsort($versions_array);
 
 
//Print the name array.
print_r($versions_array);
 
/*
OUTPUT
Array
(
    [0] => Version1 
    [1] => Version2 
    [2] => Version10
    [3] => Version11
)
*/
?>

Works fine. We also have a variant of this function and that’s natcasesort. The primary difference is that natcasesort is case insensitive. However, we leave that for you to explore. Hope you’ve enjoyed this article.

Want to explore further about PHP arrays?

We have many fun articles related to PHP arrays. You can explore these to learn more about arrays in PHP.

Print an array in PHP

Loop through an array in PHP

Check if an array is empty in PHP

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.