In the previous chapter, you learned the basics of PHP arrays: what arrays are, how to create them, how to view their structure, and how to access their elements. Arrays can do much more, such as sorting their elements in various orders.
PHP provides several built-in functions specifically designed to sort array elements in different ways, whether alphabetically or numerically, in ascending or descending order. Below are some commonly used functions for sorting arrays:
sort()
and rsort()
— for sorting indexed arraysasort()
and arsort()
— for sorting associative arrays by valueksort()
and krsort()
— for sorting associative arrays by keyThe sort()
function is used to arrange elements of an indexed array in ascending order, whether they are letters or numbers.
<?php
// Define array
$colors = array("Red", "Green", "Blue", "Yellow");
// Sorting and printing array
sort($colors);
print_r($colors);
?>
The output of print_r()
is as follows:
In the same way, you can sort the numeric elements of the array in ascending order.
<?php
// Define array
$numbers = array(1, 2, 2.5, 4, 7, 10);
// Sorting and printing array
sort($numbers);
print_r($numbers);
?>
The output of print_r()
is as follows:
The rsort()
function is used to sort the elements of an indexed array in descending order, whether they are letters or numbers.
<?php
// Define array
$colors = array("Red", "Green", "Blue", "Yellow");
// Sorting and printing array
rsort($colors);
print_r($colors);
?>
The output of print_r()
is as follows:
In the same manner, you can sort the numeric elements of the array in descending order.
<?php
// Define array
$numbers = array(1, 2, 2.5, 4, 7, 10);
// Sorting and printing array
rsort($numbers);
print_r($numbers);
?>
The output of print_r()
is as follows:
The asort()
function arranges the elements of an associative array in ascending order based on their values. It functions similarly to sort()
, but it maintains the association between keys and their corresponding values during sorting.
<?php
// Define array
$age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35);
// Sorting array by value and print
asort($age);
print_r($age);
?>
The output of print_r()
is as follows:
The arsort()
function arranges the elements of an associative array in descending order based on their values. It operates similarly to rsort()
, but maintains the association between keys and their respective values throughout the sorting process.
<?php
// Define array
$age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35);
// Sorting array by value and print
arsort($age);
print_r($age);
?>
The output of print_r()
is as follows:
The ksort()
function arranges the elements of an associative array in ascending order based on their keys. It maintains the association between keys and their values during sorting, similar to the asort()
function.
<?php
// Define array
$age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35);
// Sorting array by key and print
ksort($age);
print_r($age);
?>
The output of print_r()
is as follows:
The krsort()
function arranges the elements of an associative array in descending order based on their keys. It maintains the association between keys and their values during sorting, similar to the arsort()
function.
<?php
// Define array
$age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35);
// Sorting array by key and print
krsort($age);
print_r($age);
?>
The output of print_r()
is as follows: