Functions for array sorting in PHP

Functions for array sorting in PHP
Functions for array sorting in PHP


Functions for array sorting in PHP

In this tutorial you will learn how to sort the elements or keys of an array in ascending or descending order in PHP.
What is sorting?

Sorting is ordering data in an alphabetical, numerical order and increasing or decreasing order according to some linear relationship between the data items.

Sorting function for array
  • sort() - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order
  • ksort() - sort associative arrays in ascending order
  • arsort() - sort associative arrays in descending order
  • krsort() - sort associative arrays in descending order



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

Sort Array in Ascending Order – sort()

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


Example-

In this example sorts the elements of the $info array in ascending alphabetical order:



<!DOCTYPE html>
<html>
<head>
            <title>Sort array in ascending order - Sort()</title>
</head>
<body>
            <?php
                        $info = array('Welcome','Cyber','Teak !');
                        sort($info);
                        $length = count($info);
                        for ($i=0; $i <$length ; $i++) {
                                    echo $info[$i]."<br>";
                        }
            ?>
</body>
</html>

Output-




Example –

In this example sorts the elements of the $info array in ascending numerical order:

<!DOCTYPE html>
<html>
<head>
            <title>Sort array in ascending order - Sort()</title>
</head>
<body>
            <?php
                        $info = array(10,5,3,55,22,11,2,66,23,45);
                        sort($info);
                        $length = count($info);
                        for ($i=0; $i <$length ; $i++) {
                                    echo $info[$i]."<br>";
                        }
            ?>
</body>
</html>

Output –


Sort Array in Descending Order - rsort()

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

Example -

In this example sorts the elements of the $info array in descending alphabetical order:

<!DOCTYPE html>
<html>
<head>
            <title>Sort array in descending order - rsort()</title>
</head>
<body>
            <?php
                        $info = array('Welcome','Cyber','Teak !');
                        rsort($info);
                        $length = count($info);
                        for ($i=0; $i <$length ; $i++) {
                                    echo $info[$i]."<br>";
                        }
            ?>
</body>
</html>

Output –





Example –

In this example sorts the elements of the $info array in descending numerical order:

<!DOCTYPE html>
<html>
<head>
            <title>Sort array in descending order - rsort()</title>
</head>
<body>
            <?php
                        $info = array(10,5,3,55,22,11,2,66,23,45);
                        rsort($info);
                        $length = count($info);
                        for ($i=0; $i <$length ; $i++) {
                                    echo $info[$i]."<br>";
                        }
            ?>
</body>
</html>

Output –


Sort associative array in ascending order, According to value – asort()

The asort() function is used for sorting the elements of the associative array in ascending order.

Example –

In this example sorts an associative array in ascending order, according to value:

<!DOCTYPE html>
<html>
<head>
            <title>asort() - ascending order</title>
</head>
<body>
            <?php
                        $info = array("Ram"=>50,"Maya"=>33,"Manish"=>43);
                        asort($info);
                        //Access array
                        foreach ($info as $key => $value) {
                                    echo $key." : ".$value."<br>";
                        }
            ?>
</body>
</html>

Output –

Sort associative array in ascending order, According to key – ksort()

The ksort() function is used for sorting the elements of the associative array in ascending order.

Example –

In this example sorts an associative array in ascending order, according to key:

<!DOCTYPE html>
<html>
<head>
            <title>ksort() - ascending order</title>
</head>
<body>
            <?php
                        $info = array("Ram"=>50,"Aditya"=>33,"Manish"=>43);
                        ksort($info);
                        //Access array
                        foreach ($info as $key => $value) {
                                    echo $key." : ".$value."<br>";
                        }
            ?>
</body>
</html>

Output –


Sort associative array in descending order, According to value – arsort()

The arsort() function is used for sorting the elements of the associative array in descending order.

Example –

In this example sorts an associative array in descending order, according to value:

<!DOCTYPE html>
<html>
<head>
            <title>arsort() - descending order via value</title>
</head>
<body>
            <?php
                        $info = array("Ram"=>200,"Shyam"=>125,"Abhi"=>250);
                        //Descending order accourding values
                        arsort($info);
                        //Access associative array
                        foreach ($info as $key => $value) {
                                    echo $key." : ".$value."<br>";
                        }
            ?>
</body>
</html>

Output –


Sort associative array in descending order, According to key – krsort()

The krsort() function is used for sorting the elements of the associative array in descending order.

Example –

In this example sorts an associative array in descending order, according to key:

<!DOCTYPE html>
<html>
<head>
            <title>krsort() - descending order via key</title>
</head>
<body>
            <?php
                        $info = array("Ram"=>200,"Shyam"=>125,"Abhi"=>250);
                        //Descending order accourding key
                        krsort($info);
                        //Access associative array
                        foreach ($info as $key => $value) {
                                    echo $key." : ".$value."<br>";
                        }
            ?>
</body>
</html>

Output –


Post a Comment

0 Comments