Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP Sorting Array

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 180
    Comment on it

    Sorting Arrays

    PHP offers a host of functions for sorting arrays.

    • An initial a means the function sorts by value but maintains the key/value pairs association.
    • An initial k means the function sorts by key but maintains the key/value pairs association.
    • An initial r means the function sorts in reverse.
    • An initial missing of k or a means the function sorts by value but doesnt maintain the key/value pairs association. The numerical keys will be renumbered to reflect the new ordering.
    • An initial u means a second argument is expected as a user defined function.

    PHP- Array Sorting Functions

    • asort() sorts by value and maintains the key/value pairs associations.
    • ksort() sorts by key and maintains the key/value pairs associations.
    • arsort() sorts by value but in decendind/reverse order. Maintains the key/value pairs association.
    • krsort() sorts by key but in decending/reverse order. Maintains the key/value pairs association.
    • sort() sorts by value but does not Maintains the key/value pairs association.
    • rsort() same as sort(). But reverse the sorting order. I.e Decending.
    • uasort() sorts by value. The second argument is the user defined function. The function should return a negative number if its first argument is before the second argument, a positive number if the first argument comes after the second. And returns zero if the elements are same.
    • uksort() similar to uasort except that it sorts by keys.
    • usort() similar to uasort. But it does not maintain the key/value pair relationships.

    Example: asort()

    <?php
    $arr = array('English', 'Maths', 'Hindi', 'science', 'GK', 'ENglish');
    echo '--------Array 1----------------';
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    echo '--------Array 2----------------';
    asort($arr);
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    ?>
    

    Output:

    --------Array 1----------------
        Array
        (
            [0] => English
            [1] => Maths
            [2] => Hindi
            [3] => science
            [4] => GK
            [5] => ENglish
        )
    --------Array 2----------------
        Array
        (
            [5] => ENglish
            [0] => English
            [4] => GK
            [2] => Hindi
            [1] => Maths
            [3] => science
        )
    

    Example: arsort()

    <?php 
    $arr = array('ONE' => 'English', 'TWO' => 'Maths', 'THREE' => 'Hindi', 'FOUR' => 'science', 'FIVE' => 'GK', 'SIX' => 'ENglish');
        echo '-----------Array1--------------';
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
        echo '-----------Array2--------------';
        arsort($arr);
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
        ?>
    

    Output:

    -----------Array1--------------
        Array
        (
            [ONE] => English
            [TWO] => Maths
            [THREE] => Hindi
            [FOUR] => science
            [FIVE] => GK
            [SIX] => ENglish
        )
    
    -----------Array2--------------
        Array
        (
            [FOUR] => science
            [TWO] => Maths
            [THREE] => Hindi
            [FIVE] => GK
            [ONE] => English
            [SIX] => ENglish
        )
    

    Example:- ksort()

       <?php
        $arr = array('ONE' => 'English', 'TWO' => 'Maths', 'THREE' => 'Hindi', 'FOUR' => 'science', 'FIVE' => 'GK', 'SIX' => 'ENglish');
        echo '-----------Array1--------------';
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
    
        echo '-----------Array2--------------';
        ksort($arr);
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
        ?>
    

    Output

    -----------Array1--------------
    Array
    (
        [ONE] => English
        [TWO] => Maths
        [THREE] => Hindi
        [FOUR] => science
        [FIVE] => GK
        [SIX] => ENglish
    )
    -----------Array2--------------
    Array
    (
        [FIVE] => GK
        [FOUR] => science
        [ONE] => English
        [SIX] => ENglish
        [THREE] => Hindi
        [TWO] => Maths
    )
    

    Example: krsort()

       <?php
    $arr = array('ONE' => 'English', 'TWO' => 'Maths', 'THREE' => 'Hindi', 'FOUR' => 'science', 'FIVE' => 'GK', 'SIX' => 'ENglish');
        echo '-----------Array1--------------';
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
        echo '-----------Array2--------------';
        krsort($arr);
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
       ?>
    

    Output:

    -----------Array1--------------
    Array
    (
        [ONE] => English
        [TWO] => Maths
        [THREE] => Hindi
        [FOUR] => science
        [FIVE] => GK
        [SIX] => ENglish
    )
    -----------Array2--------------
    Array
    (
        [TWO] => Maths
        [THREE] => Hindi
        [SIX] => ENglish
        [ONE] => English
        [FOUR] => science
        [FIVE] => GK
    )
    

    Example: sort()

     <?php
    $arr = array('ONE' => 'English', 'TWO' => 'Maths', 'THREE' => 'Hindi', 'FOUR' => 'science', 'FIVE' => 'GK', 'SIX' => 'ENglish');
        echo '-----------Array1--------------';
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
        echo '-----------Array2--------------';
        sort($arr);
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
       ?>
    

    Output

    -----------Array1--------------
    Array
    (
        [ONE] => English
        [TWO] => Maths
        [THREE] => Hindi
        [FOUR] => science
        [FIVE] => GK
        [SIX] => ENglish
    )
    -----------Array2--------------
    Array
    (
        [0] => ENglish
        [1] => English
        [2] => GK
        [3] => Hindi
        [4] => Maths
        [5] => science
    )
    

    Example: rsort()

    <?php
    $arr = array('ONE' => 'English', 'TWO' => 'Maths', 'THREE' => 'Hindi', 'FOUR' => 'science', 'FIVE' => 'GK', 'SIX' => 'ENglish');
    echo '-----------Array1--------------';
    sort($arr);
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    echo '-----------Array2--------------';
    rsort($arr);
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    ?>
    

    Output:

    -----------Array1--------------
    Array
    (
        [0] => ENglish
        [1] => English
        [2] => GK
        [3] => Hindi
        [4] => Maths
        [5] => science
    )
    -----------Array2--------------
    Array
    (
        [0] => science
        [1] => Maths
        [2] => Hindi
        [3] => GK
        [4] => English
        [5] => ENglish
    )
    

    Example: usort()

     <?php
    $arr = array('ONE' => 'English', 'TWO' => 'Maths', 'THREE' => 'Hindi', 'FOUR' => 'science', 'FIVE' => 'GK', 'SIX' => 'ENglish');
    
    echo '-----------Array1--------------';
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    echo '-----------Array2--------------';
    function compare($a, $b){
        if($a == $b){
            return 0;
        }elseif($a < $b){
            return -1;
        }else{
            return 1;
    
    
    }
    }
    usort($arr, "compare");
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    ?>
    

    Output:

    -----------Array1--------------
    Array
    (
        [ONE] => English
        [TWO] => Maths
        [THREE] => Hindi
        [FOUR] => science
        [FIVE] => GK
        [SIX] => ENglish
    )
    -----------Array2--------------
    Array
    (
        [0] => ENglish
        [1] => English
        [2] => GK
        [3] => Hindi
        [4] => Maths
        [5] => science
    )
    

    Example: uasort()

    <?php
    $arr = array('ONE' => 'English', 'TWO' => 'Maths', 'THREE' => 'Hindi', 'FOUR' => 'science', 'FIVE' => 'GK', 'SIX' => 'ENglish');
        echo '-----------Array1--------------';
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
        echo '-----------Array2--------------';
        function compare($a, $b){
            if($a == $b){
                return 0;
            }elseif($a < $b){
                return -1;
    
    
     }else{
                return 1;
            }
        }
        uasort($arr, "compare");
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
    ?>
    

    Output:

    -----------Array1--------------
    
    Array
    (
        [ONE] => English
        [TWO] => Maths
        [THREE] => Hindi
        [FOUR] => science
        [FIVE] => GK
        [SIX] => ENglish
    )
    
    -----------Array2--------------
    
    Array
    (
        [SIX] => ENglish
        [ONE] => English
        [FIVE] => GK
        [THREE] => Hindi
        [TWO] => Maths
        [FOUR] => science
    )
    

    Example: uksort()

     <?php 
    $arr = array('ONE' => 'English', 'TWO' => 'Maths', 'THREE' => 'Hindi', 'FOUR' => 'science', 'FIVE' => 'GK', 'SIX' => 'ENglish');
        echo '-----------Array1--------------';
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
        echo '-----------Array2--------------';
        function compare($a, $b){
            if($a == $b){
                return 0;
            }elseif($a < $b){
                return -1;
            }else{
                return 1;
            }
        }
        uksort($arr, "compare");
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
    ?>
    

    OUTPUT:

    -----------Array1--------------
    
    Array
    (
        [ONE] => English
        [TWO] => Maths
        [THREE] => Hindi
        [FOUR] => science
        [FIVE] => GK
        [SIX] => ENglish
    )
    
    -----------Array2--------------
    
    Array
    (
        [FIVE] => GK
        [FOUR] => science
        [ONE] => English
        [SIX] => ENglish
        [THREE] => Hindi
        [TWO] => Maths
    )
    

    Example: usort()

     <?php
    $arr = array('ONE' => 'English', 'TWO' => 'Maths', 'THREE' => 'Hindi', 'FOUR' => 'science', 'FIVE' => 'GK', 'SIX' => 'ENglish');
        echo '-----------Array1--------------';
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
        echo '-----------Array2--------------';
        function compare($a, $b){
            if($a == $b){
                return 0;
            }elseif($a < $b){
                return -1;
            }else{
                return 1;
            }
        }
        usort($arr, "compare");
        echo "<pre>";
        print_r($arr);
        echo "</pre>";<?php
    ?>
    

    OUTPUT:

    -----------Array1--------------
    
    Array
    (
        [ONE] => English
        [TWO] => Maths
        [THREE] => Hindi
        [FOUR] => science
        [FIVE] => GK
        [SIX] => ENglish
    )
    
    -----------Array2--------------
    
    Array
    (
        [0] => ENglish
        [1] => English
        [2] => GK
        [3] => Hindi
        [4] => Maths
        [5] => science
    )
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: