Sorting means ordering of data in alphabetical or numeric order either in increasing or decreasing fashion according to some linear relationship among the data items. Sorting increases the searching efficiency in the array.
PHP has some built-in functions for sorting array elements in alphabetic or numeric order.
In this tutorial, we are going to explain some of the basic PHP functions for sorting array.
- sort() – Sorts indexed array in ascending order.
- rsort() – Sorts indexed array in descending order.
- asort() – Sorts associative array in ascending order, according to the value.
- arsort() – Sorts associative array in descending order, according to the value.
- ksort() – Sorts associative array in ascending order, according to the Key.
- krsort() – Sorts associative array in descending order, according to the Key.
Sorting Indexed Array in Ascending Order
The sort()
function in PHP is used to sort indexed array in ascending order. (Alphabetically for letters and numerically for numbers)
Example
<?php // Defining array $names = array("Sagar", "Anamitra", "Bimal", "Raju"); // Sorting and printing array sort($names); print_r($names); echo "<br/>$names[0] <br/>$names[1] <br/>$names[2] <br/>$names[3] "; ?>
Output
Anamitra
Bimal
Raju
Sagar
Similarly, you can sort the numerical array in ascending order.
Example
<?php // Defining array $numbers = array(8, 2, 1, 3.5, 5, 12, 11); // Sorting and printing array sort($numbers); print_r($numbers); echo "<br/>"; echo "$numbers[0] <br/> $numbers[1] <br/> $numbers[2] <br/> $numbers[3] <br/> $numbers[4] <br/> $numbers[5] <br/> $numbers[6]"; ?>
Output
1
2
3.5
5
8
11
12
Sorting Indexed Arrays in Descending Order
The rsort()
function is used to sort the indexed array in descending order. (Alphabetically for letters and numerically for numbers)
Example
<?php // Defining array $names = array("Sagar", "Anamitra", "Bimal", "Raju"); // Sorting and printing array rsort($names); print_r($names); echo "<br/>$names[0] <br/>$names[1] <br/>$names[2] <br/>$names[3] "; ?>
Output
Sagar
Raju
Bimal
Anamitra
Similarly, you can sort the numeric array in descending order.
Example
<?php // Defining array $numbers = array(8, 2, 1, 3.5, 5, 12, 11); // Sorting and printing array rsort($numbers); print_r($numbers); echo "<br/>"; echo "$numbers[0] <br/> $numbers[1] <br/> $numbers[2] <br/> $numbers[3] <br/> $numbers[4] <br/> $numbers[5] <br/> $numbers[6]"; ?>
Output
12
11
8
5
3.5
2
1
Sorting Associative Arrays in Ascending Order By Value
The asort()
function is used to sorts the elements of an associative array in ascending order according to the value. It works same as sort()
, but it preserves the association between keys and their values while sorting.
Example
<?php // Defining array $age = array("Sagar"=>34, "Anamitra"=>23, "Bimal"=>25, "Raju"=>32); // Sorting and printing array asort($age); print_r($age); foreach($age as $x => $x_value) { echo "<br>"; echo "Key=" . $x . ", Value=" . $x_value; } ?>
Output
Key=Anamitra, Value=23
Key=Bimal, Value=25
Key=Raju, Value=32
Key=Sagar, Value=34
Sorting Associative Arrays in Descending Order By Value
The arsort()
function is used sorts the elements of an associative array in descending order according to the value. It works same as rsort()
, but it preserves the association between keys and its values while sorting.
Example
<?php // Defining array $age = array("Sagar"=>34, "Anamitra"=>23, "Bimal"=>25, "Raju"=>32); // Sorting and printing array arsort($age); print_r($age); foreach($age as $x => $x_value) { echo "<br>"; echo "Key=" . $x . ", Value=" . $x_value; } ?>
Output
Key=Sagar, Value=34
Key=Raju, Value=32
Key=Bimal, Value=25
Key=Anamitra, Value=23
Sorting Associative Arrays in Ascending Order By Key
The ksort()
function sorts the elements of an associative array in ascending order according to their keys. It also preserves the association between keys and its values while sorting, same as asort()
function.
Example
<?php // Defining array $age = array("Sagar"=>34, "Anamitra"=>23, "Bimal"=>25, "Raju"=>32); // Sorting and printing array ksort($age); print_r($age); foreach($age as $x => $x_value) { echo "<br>"; echo "Key=" . $x . ", Value=" . $x_value; } ?>
Output
Key=Anamitra, Value=23
Key=Bimal, Value=25
Key=Raju, Value=32
Key=Sagar, Value=34
Sorting Associative Arrays in Descending Order By Key
The krsort()
function sorts the elements of an associative array in descending order according to their keys. It also preserves the association between keys and its values while sorting, same as arsort()
function.
Example
<?php // Defining array $age = array("Sagar"=>34, "Anamitra"=>23, "Bimal"=>25, "Raju"=>32); // Sorting and printing array krsort($age); print_r($age); foreach($age as $x => $x_value) { echo "<br>"; echo "Key=" . $x . ", Value=" . $x_value; } ?>
Output
Key=Sagar, Value=34
Key=Raju, Value=32
Key=Bimal, Value=25
Key=Anamitra, Value=23