English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to sort the elements or keys of an array in ascending or descending order in PHP.
In the previous chapter, you have learned the basic knowledge of PHP arrays, that is, what an array is, how to create them, how to view their structure, how to access their elements, etc. You can do more with arrays, such as sorting elements in the order you specify.
PHP comes with many built-in functions that are specifically designed to sort array elements in different ways, such as in alphabetical or numerical order, in ascending or descending order. Here, we will discuss some of the most commonly used functions for array sorting.
sort() and rsort() - Sort indexed arrays
asort() and arsort() - Used to sort associative arrays by value
ksort() and krsort() - Used to sort associative arrays by key
The sort() function is used to sort the elements of an indexed array in ascending order (letters are sorted alphabetically, numbers are sorted numerically).
<?php //Define an array $colors = array("Red", "Green", "Blue", "Yellow"); //Sort and print the array sort($colors); print_r($colors); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [0] => Blue [1] => Green [2] => Red [3] => Yellow )
Similarly, you can sort the numeric elements of an array in ascending order.
<?php //Define an array $numbers = array(1, 2, 2.5, 4, 7, 10); //Sort and print the array sort($numbers); print_r($numbers); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [0] => 1 [1] => 2 [2] => 2.5 [3] => 4 [4] => 7 [5] => 10 )
The rsort() function is used to sort the elements of an indexed array in descending order (letters are sorted alphabetically, numbers are sorted numerically).
<?php //Define an array $colors = array("Red", "Green", "Blue", "Yellow"); // Sort and print the array rsort($colors); print_r($colors); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [0] => Yellow [1] => Red [2] => Green [3] => Blue )
Similarly, you can sort the numeric elements of an array in descending order.
<?php //Define an array $numbers = array(1, 2, 2.5, 4, 7, 10); //Sort and print the array rsort($numbers); print_r($numbers); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [0] => 10 [1] => 7 [2] => 4 [3] => 2.5 [4] => 2 [5] => 1 )
The asort() function sorts the elements of an associative array in ascending order by value. It works similarly to sort(), but it preserves the association between keys and their values during sorting.
<?php //Define an array $age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35); //Sort and print the array by value asort($age); print_r($age); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [Harry] => 14 [Peter] => 20 [Clark] => 35 [John] => 45 )
The arsort() function sorts the elements of an associative array in descending order by value. It works similarly to rsort(), but it preserves the association between keys and their values during sorting.
<?php //Define an array $age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35); //Sort and print the array by value arsort($age); print_r($age); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [John] => 45 [Clark] => 35 [Peter] => 20 [Harry] => 14 )
The ksort() function sorts the elements of an associative array in ascending order by key. Like the asort() function, it preserves the association between keys and their values during sorting.
<?php //Define an array $age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35); //Sort the array by key and print ksort($age); print_r($age); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [Clark] => 35 [Harry] => 14 [John] => 45 [Peter] => 20 )
The ksort() function sorts the elements of an associative array in ascending order by key. Like the asort() function, it preserves the association between keys and their values during sorting.
<?php //Define an array $age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35); //Sort the array by key and print krsort($age); print_r($age); ?>Test and see‹/›
print_r() statement provides the following output:
Array ( [Peter] => 20 [John] => 45 [Harry] => 14 [Clark] => 35 )