The elements of an array can be sorted alphabetically or numerically in ascending or descending order.
PHP Functions For Sorting Arrays
In the previous chapter you've learnt the essentials of PHP arrays i.e. what arrays are, how to create them, how to view their structure, how to access their elements etc. You can do even more things with arrays like sorting the elements in any order you like.
PHP comes with a number of built-in functions designed specifically for sorting array elements in different ways like alphabetically or numerically in ascending or descending order. In this tutorial we will explore some of these functions most commonly used for sorting arrays.
sort()
andrsort()
— For sorting indexed arraysasort()
andarsort()
— For sorting associative arrays by valueksort()
andkrsort()
— 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 (alphabetically for letters and numerically for numbers).Example
// Define array
$colors = array("Red", "Green", "Blue", "Yellow");
// Sorting and printing array
sort($colors);
print_r($colors);
This
print_r()
statement gives the following output:
Similarly you can sort the numeric elements of the array in ascending order.
Example
// Define array
$numbers = array(1, 2, 2.5, 4, 7, 10);
// Sorting and printing array
sort($numbers);
print_r($numbers);
This
print_r()
statement gives the following output:Sorting Indexed Arrays in Descending Order
The
rsort()
function is used for sorting the elements of the indexed array in descending order (alphabetically for letters and numerically for numbers).Example
// Define array
$colors = array("Red", "Green", "Blue", "Yellow");
// Sorting and printing array
rsort($colors);
print_r($colors);
This
print_r()
statement gives the following output:
Similarly you can sort the numeric elements of the array in descending order.
Example
// Define array
$numbers = array(1, 2, 2.5, 4, 7, 10);
// Sorting and printing array
rsort($numbers);
print_r($numbers);
This
print_r()
statement gives the following output:Sorting Associative Arrays in Ascending Order By Value
The
asort()
function sorts the elements of an associative array in ascending order according to the value. It works just like sort()
, but it preserves the association between keys and its values while sorting.Example
// Define array
$age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35);
// Sorting array by value and print
asort($age);
print_r($age);
This
print_r()
statement gives the following output:Sorting Associative Arrays in Descending 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 it preserves the association between keys and its values while sorting.Example
// Define array
$age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35);
// Sorting array by value and print
arsort($age);
print_r($age);
This
print_r()
statement gives the following output:Sorting Associative Arrays in Ascending Order By Key
The
ksort()
function sorts the elements of an associative array in ascending order ascending to the key. It preserves the association between keys and its values while sorting, same as asort()
function.Example
// Define array
$age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35);
// Sorting array by value and print
ksort($age);
print_r($age);
This
print_r()
statement gives the following output:Sorting Associative Arrays in Descending Order By Key
The
krsort()
function sorts the elements of an associative array in descending order ascending to the key. It preserves the association between keys and its values while sorting, same as arsort()
function.Example
// Define array
$age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35);
// Sorting array by value and print
krsort($age);
print_r($age);
This
print_r()
statement gives the following output: