PHP comes with several built-in functions, specifically for sorting array elements such as alphabetically, ascending, descending order. Here we will explore some of the most commonly used ordering functions.


  • sort() and rsort() — For sorting indexed arrays
  • asort() and arsort() — For sorting associative arrays by value
  • ksort() and krsort() — For sorting associative arrays by key

Sorting Indexed Arrays in Ascending Order

The sort() function is used to sort the elements of the indexed array in ascending order.


#example.php

<?php
// 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:

Array ( [0] => Blue [1] => Green [2] => Red [3] => Yellow )

Similarly, 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);
?>

Sorting Indexed Arrays in Descending Order

The rsort() function is used for sorting the elements of the indexed array in descending order


<?php
// 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:

Array ( [0] => Yellow [1] => Red [2] => Green [3] => Blue )

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.

<?php
// Define array
$age = array("Rathorji"=>20, "Jhon"=>14, "Rakesh"=>45, "Pintoo"=>35);
 
// Sorting array by value and print
asort($age);
print_r($age);
?>


This print_r() statement gives the following output:

Array ( [Jhon] => 14 [Rathorji] => 20 [Pintoo] => 35 [Rakesh] => 45 )

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. 

<?php
// Define array
$age = array("Rathorji"=>20, "Jhon"=>14, "Rakesh"=>45, "Pintoo"=>35);
 
// Sorting array by value and print
arsort($age);
print_r($age);
?>

This print_r() statement gives the following output:

Array ( [Rakesh] => 45 [Pintoo] => 35 [Rathorji] => 20 [Jhon] => 14 )

Sorting Associative Arrays in Ascending Order By Key

The ksort() function sorts the elements of an associative array in ascending order by their keys.


<?php
// Define array
$age = array("Rathorji"=>20, "Jhon"=>14, "Rakesh"=>45, "Pintoo"=>35);
 
// Sorting array by value and print
ksort($age);
print_r($age);
?>

This print_r() statement gives the following output:

Array ( [Jhon] => 14 [Pintoo] => 35 [Rakesh] => 45 [Rathorji] => 20 )

Sorting Associative Arrays in Descending Order By Key

The krsort() function sorts the elements of an associative array in descending order by their keys.


<?php
// Define array
$age = array("Rathorji"=>20, "Jhon"=>14, "Rakesh"=>45, "Pintoo"=>35);
 
// Sorting array by value and print
krsort($age);
print_r($age);
?>

This print_r() statement gives the following output:

Array ( [Rathorji] => 20 [Rakesh] => 45 [Pintoo] => 35 [Jhon] => 14 )