Sorting Array in PHP
In this tutorial, we will learn about sorting an array in PHP using various built-in sorting functions provided by PHP. Sorting is a process of arranging elements in a specific order – either in ascending or descending.
It is one of the most common operations performed on PHP arrays. PHP provides several built-in functions for sorting an array. You can sort arrays by their values or keys, with or without maintaining the original index association.
Sorting Array in PHP by Values
Arranging array elements based on their values is called sorting an array by values. PHP provides many built-in functions for sorting an array based on values. They are:
- sort()
- rsort()
- asort()
- arsort()
Let’s discuss each sorting function one by one with examples.
1. sort() Function
The sort() function in PHP sorts array elements in ascending order based on their values. This function does not maintain the index association, meaning that the original keys (indices) of the array are reindexed sequentially, starting from 0.
In simple words, the sort() function only sorts the values of the array, and does not preserve the original keys of the array. The general syntax of sort() function is as:
sort(array &$array, int $flags = SORT_REGULAR): bool
In the above syntax,
- $array: It represents an array that has to be sorted. It is passed by reference, so the array is modified directly.
- $flags: This is an optional parameter that determines how the values are compared. Some common flags include:
- SORT_REGULAR (default): Compares items normally.
- SORT_NUMERIC: Compares items numerically.
- SORT_STRING: Compares items as strings.
- SORT_NATURAL: Compares items in natural order.
- SORT_FLAG_CASE: Used with SORT_STRING for case-insensitive sorting.
The sort() function in PHP returns a boolean value:
- true: If the sorting is successful.
- false: If the sorting fails due to an error.
Example 1: Sorting in Ascending Order
<?php
$array = [4, 2, 8, 1];
sort($array);
print_r($array);
?>
Output: Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 8 )
Example 2: Sorting Strings
<?php
$array = ["apple", "cherry", "banana", "date"];
sort($array);
print_r($array);
?>
Output: Array ( [0] => apple [1] => banana [2] => cherry [3] => date )
In this example, we have sorted array elements based on string values.
Example 3: Numeric Sorting
<?php
$array = ["20", "5", "100", "50"];
sort($array, SORT_NUMERIC);
print_r($array);
?>
Output: Array ( [0] => 5 [1] => 20 [2] => 50 [3] => 100 )
Example 4: Natural Order Sorting
<?php
$array = ["img12.png", "img2.png", "img1.png", "img10.png"];
sort($array, SORT_NATURAL);
print_r($array);
?>
Output: Array ( [0] => img1.png [1] => img2.png [2] => img10.png [3] => img12.png )
Indexed Array (Keys reindexing)
Example 5:
<?php
$array = [3 => 20, 5 => 2, 7 => 30];
sort($array);
print_r($array);
?>
Output: Array ( [0] => 2 [1] => 20 [2] => 30 )
In this example, before sorting:
- Key 3 had a value 20,
- Key 5 had a value 2,
- Key 7 had a value 30.
After Sorting:
- The values of the array are sorted, but the keys (3, 5, 7) are lost. New keys (0, 1, 2) are assigned in sequential order.
Associative Array (Keys are not preserved)
Example 6:
<?php
$array = ["a" => 50, "b" => 20, "c" => 40];
sort($array);
print_r($array);
?>
Output: Array ( [0] => 20 [1] => 40 [2] => 50 )
In this example, keys a, b, c mapped to values 50, 20, 40 before sorting. After sorting, the values are sorted, but the keys (a, b, c) are not preserved. New numeric indices are assigned (0, 1, 2) in the sequential order.
2. rsort() Function
The rsort() function in PHP sorts an array by its values in descending order. Similar to the sort() function, it also does not maintain the index association of the array. Therefore, this function is unsuitable for associative arrays where keys are important. The general syntax of this function is as:
rsort(array &$array, int $flags = SORT_REGULAR): bool
Example 7: Sorting Numbers
<?php
$numbers = [3, 1, 4, 1, 5];
rsort($numbers);
print_r($numbers);
?>
Output: Array ( [0] => 5 [1] => 4 [2] => 3 [3] => 1 [4] => 1 )
Example 8: Sorting Strings
<?php
$fruits = ["Banana", "Apple", "Mango", "Orange"];
rsort($fruits);
print_r($fruits);
?>
Output: Array ( [0] => Orange [1] => Mango [2] => Banana [3] => Apple )
In this example, we have sorted array elements of string type based on their values.
Example 9: Sorting with Flags
<?php
$values = ["10", "2", "30", "22"];
rsort($values, SORT_NUMERIC);
print_r($values);
?>
Output: Array ( [0] => 30 [1] => 22 [2] => 10 [3] => 2 )
Example 10: Sorting with Mixed Types
<?php
$mixed = [3, "20", "PHP", 15];
rsort($mixed, SORT_STRING);
print_r($mixed);
?>
Output: Array ( [0] => PHP [1] => 3 [2] => 20 [3] => 15 )
3. asort() Function
The asort() function in PHP sorts an array by its values in ascending order while maintaining the original key-value association of the array. This makes it useful for associative arrays, where the relationship between keys and values is important. Its syntax is similar to sort() function.
Example 11: Sorting a Simple Associative Array
<?php
$ages = ["Alice" => 24, "Bob" => 19, "Charlie" => 33];
asort($ages);
print_r($ages);
?>
Output: Array ( [Bob] => 19 [Alice] => 24 [Charlie] => 33 )
Example 12: Sorting with Strings
<?php
$fruits = ["a" => "Banana", "b" => "Apple", "c" => "Cherry"];
asort($fruits);
print_r($fruits);
?>
Output: Array ( [b] => Apple [a] => Banana [c] => Cherry )
As you can see in both examples, the asort() function sorts the array elements based on their values in ascending order while maintaining the original key-value association.
Example 13: Using SORT_NUMERIC Flag
<?php
$values = ["a" => "10", "b" => "2", "c" => "30"];
asort($values, SORT_NUMERIC);
print_r($values);
?>
Output: Array ( [b] => 2 [a] => 10 [c] => 30 )
Example 14: Mixed Types
<?php
$items = ["a" => "AAA", "b" => 10, "c" => "2"];
asort($items, SORT_NUMERIC);
print_r($items);
?>
Output: Array ( [a] => AAA [c] => 2 [b] => 10 )
4. arsort() Function
The arsort() function in PHP sorts an array by its values in descending order while maintaining the original key-value mapping of the array. This function is useful while working with associative arrays where the order of values matters, but you want to preserve the original keys.
Example 15: Sorting an Associative Array in Descending Order
<?php
$ages = ["Alice" => 25, "Bob" => 20, "Charlie" => 35];
arsort($ages);
print_r($ages);
?>
Output: Array ( [Charlie] => 35 [Alice] => 25 [Bob] => 20 )
Example 16: Sorting with Strings
<?php
$name = ["a" => "Bob", "b" => "Saanvi", "c" => "John"];
arsort($name);
print_r($name);
?>
Output: Array ( [b] => Saanvi [c] => John [a] => Bob )
Here, you have learned about sorting an array in PHP by values with various examples. Stay tuned with the next tutorial, where you will learn about sorting an associative array in PHP by its keys.