PHP Tutorial PHP Advanced PHP References

PHP - Sorting Arrays



In PHP, there are built-in functions which can be used to sort arrays. In this section, we will discuss PHP sort functions:

FunctionsDescription
sort()Sorts an array in ascending order.
rsort()Sorts an array in descending order.
asort()Sorts an array in ascending order, according to the value and maintaining key-value association. Mainly used for sorting associative arrays where the actual element order is significant.
arsort()Sorts an array in descending order, according to the value and maintaining key-value association. Mainly used for sorting associative arrays where the actual element order is significant.
ksort()Sorts an array in ascending order, according to the key and maintaining key-value association. Mainly used for sorting associative arrays where the actual element order is significant.
krsort()Sorts an array in descending order, according to the key and maintaining key-value association. Mainly used for sorting associative arrays where the actual element order is significant.

Sort Array in Ascending Order - sort()

In the example below, sort() function is used to sort the given indexed array in ascending order.

<?php
$MyArray = array("Marry", "John", "Kim", "Adam");

//using sort() function to sort
//the array in ascending order
sort($MyArray);

//displaying the result
print_r($MyArray);
?>

The output of the above code will be:

Array
(
    [0] => Adam
    [1] => John
    [2] => Kim
    [3] => Marry
)

Sort Array in Descending Order - rsort()

Consider the example below where rsort() function is used to sort the given indexed array in descending order.

<?php
$MyArray = array("Marry", "John", "Kim", "Adam");

//using rsort() function to sort
//the array in descending order
rsort($MyArray);

//displaying the result
print_r($MyArray);
?>

The output of the above code will be:

Array
(
    [0] => Marry
    [1] => Kim
    [2] => John
    [3] => Adam
)

Sort Array in Ascending Order by Value - asort()

In the example below, asort() function is used to sort the given associative array in ascending order, according to its value.

<?php
$MyArray = array("Marry"=>25, 
                 "John"=>30, 
                 "Kim"=>22, 
                 "Adam"=>35);

//using asort() function to sort the
//array in ascending order by value
asort($MyArray);

//displaying the result
print_r($MyArray);
?>

The output of the above code will be:

Array
(
    [Kim] => 22
    [Marry] => 25
    [John] => 30
    [Adam] => 35
)

Sort Array in Descending Order by Value - arsort()

Consider the example below where arsort() function is used to sort the given associative array in descending order, according to its value.

<?php
$MyArray = array("Marry"=>25, 
                 "John"=>30, 
                 "Kim"=>22, 
                 "Adam"=>35);

//using arsort() function to sort the
//array in descending order by value
arsort($MyArray);

//displaying the result
print_r($MyArray);
?>

The output of the above code will be:

Array
(
    [Adam] => 35
    [John] => 30
    [Marry] => 25
    [Kim] => 22
)

Sort Array in Ascending Order by Key - ksort()

In the example below, ksort() function is used to sort the given associative array in ascending order, according to its key.

<?php
$MyArray = array("Marry"=>25, 
                 "John"=>30, 
                 "Kim"=>22, 
                 "Adam"=>35);

//using ksort() function to sort the
//array in ascending order by key 
ksort($MyArray);

//displaying the result
print_r($MyArray);
?>

The output of the above code will be:

Array
(
    [Adam] => 35
    [John] => 30
    [Kim] => 22
    [Marry] => 25
)

Sort Array in Descending Order by Key - krsort()

Consider the example below where krsort() function is used to sort the given associative array in descending order, according to its key.

<?php
$MyArray = array("Marry"=>25, 
                 "John"=>30, 
                 "Kim"=>22, 
                 "Adam"=>35);

//using krsort() function to sort the
//array in descending order by key 
krsort($MyArray);

//displaying the result
print_r($MyArray);
?>

The output of the above code will be:

Array
(
    [Marry] => 25
    [Kim] => 22
    [John] => 30
    [Adam] => 35
)