How to sort an associative array by values and by keys in PHP

An associative array has key and value pair as elements in the form $key => $value. You can sort an associative array by values or by keys. asort() and ksort() are used for ascending order by values and by keys respectively. arsort() and krsort() are used for descending order by values and by keys respectively.

In this topic, we will see how we can use asort(), arsort(), ksort() and krsort() PHP functions on the array and display the elements in ascending and descending order of values and keys. See below code how these are used in PHP code:

Here, we have an associative array $languages with key and values, and I am using the sort functions on the array. If I use var_dump() to display the new order after each sort function is applied on the array, output for each function is displayed as below:

$languages = array('a'=>'PHP','b'=>'Basic','c'=>'Pascal','d'=>'Python','e'=>'Java');

asort($languages) - sorts array in ascending order of values

array(5) { ["b"]=> string(5) "Basic" ["e"]=> string(4) "Java" ["a"]=> string(3) "PHP" ["c"]=> string(6) "Pascal" ["d"]=> string(6) "Python" }

arsort($languages) - sorts array in descending order of values

array(5) { ["d"]=> string(6) "Python" ["c"]=> string(6) "Pascal" ["a"]=> string(3) "PHP" ["e"]=> string(4) "Java" ["b"]=> string(5) "Basic" }

ksort($languages) - sorts array in ascending order of key

array(5) { ["a"]=> string(3) "PHP" ["b"]=> string(5) "Basic" ["c"]=> string(6) "Pascal" ["d"]=> string(6) "Python" ["e"]=> string(4) "Java" }

krsort($languages) - sorts array in descending order of key

array(5) { ["e"]=> string(4) "Java" ["d"]=> string(6) "Python" ["c"]=> string(6) "Pascal" ["b"]=> string(5) "Basic" ["a"]=> string(3) "PHP" }

Now, let us write a program which will display original array and the sorted array after applying the above functions on the array. Let's take a look at below code:

The keys are 'a', 'b', 'c', 'd' and 'e'. Values associated with each key are also given. We want to reorder the elements of the array by keys and by values in ascending and descending order. We are displaying the original array using a foreach loop in line 17. Then we used asort($languages) to sort it in ascending order of values and displaying the array elements.

Similarly, using arsort($languages), ksort($languages) and krsort($languages) and displaying the elements each time we apply the array functions.

We created a folder named 'array' under xampp/htdocs. Then saved this file as index.php under xampp/htdoc/array.

If we run this in the browser using localhost/array, output is displayed as below:

sort associative array in php

So, this was an example of how you can sort and display an associative array in PHP.

PHP Sort Array by Key and value with exampleConclusion

In this simple topic, I have tried to show how you can sort associative array using PHP functions. It all depends on your requirement for which you can apply these functions to sort elements of an associative array either by keys or by values.