PHP Sorting Arrays

PHP Functions For Sorting Arrays

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 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 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:

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

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:

Array ( [0] => 1 [1] => 2 [2] => 2.5 [3] => 4 [4] => 7 [5] => 10 )

Sorting Indexed Arrays in Descending Order

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:

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

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:

Array ( [0] => 10 [1] => 7 [2] => 4 [3] => 2.5 [4] => 2 [5] => 1 )

Sorting Associative Arrays in Ascending Order By Value

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:

Array ( [Harry] => 14 [Peter] => 20 [Clark] => 35 [John] => 45 )

Sorting Associative Arrays in Descending Order By Value

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:

Array ( [John] => 45 [Clark] => 35 [Peter] => 20 [Harry] => 14 )

Sorting Associative Arrays in Ascending Order By Key

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:

Array ( [Clark] => 35 [Harry] => 14 [John] => 45 [Peter] => 20 )

Sorting Associative Arrays in Descending Order By Key

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:

Array ( [Peter] => 20 [John] => 45 [Harry] => 14 [Clark] => 35 )