Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What are the different array functions in sorting an array?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 110
    Comment on it

    Hello Readers

    Below is the Sorting functions in PHP:

    1. sort(): It is an array function which is used to sorts the array and it will be arranged the elements in ascending order (lowest to highest).

    Example of sort():

    <?php  
    $subject = array("Language",  "English", "Math", "History");  
    sort($subject);  
    foreach ($subject as $key => $val)  
    {  
    echo "subject[ " . $key . "] = " . $val . "<br />";  
    }  
    ?>
    

    Output :
    subject[ 0] = English
    subject[ 1] = History
    subject[ 2] = Language
    subject[ 3] = Math

    1. rsort(): It is an array function which is used to sorts the array and it will be arranged the elements in descending order (highest to lowest). It is basically the reverse of sort() function.

    Example of rsort():

    <?php
    $subject = array("Language", "English", "Math", "History");  
    rsort($subject);  
    foreach ($subject as $key => $val)  
    {  
    echo "subject[ " . $key . "] = " . $val . "<br />";  
    }
    ?>
    

    Output :
    subject[ 0] = Math
    subject[ 1] = Language
    subject[ 2] = History
    subject[ 3] = English

    1. asort(): It is an array function which is used to sorts an array and this function maintain index association.

    Example of asort():

    <?php  
    $subject = array('d' => 'Language',  'c' => 'Math',  'a' => 'Science', 'b'=> 'Geography');  
    asort($subject);  
    foreach ($subject as $key => $val)  
    {  
    echo "$key = $val <br />";  
    }
    ?>
    

    Output :
    b = Geography
    d = Language
    c = Math
    a = Science

    1. arsort(): It is an array function which is used to sorts an array in reverse order. This type of function also maintains index association as like asort(). It is a reverse of asort() function.

    Example of arsort():

    <?php
    $subject = array('d' => 'Language',  'c' => 'Math',  'a' => 'Science', 'b'=> 'Geography');  
    arsort($subject);  
    foreach ($subject as $key=>$val)  
    {  
    echo "$key = $val <br />";  
    }
    ?>
    

    Output :
    a = Science
    c = Math
    d = Language b = Geography

    1. ksort(): It is an array function which is used to sorts an array but it sorts an array in associative array in ascending order, according to the key.

    Example of ksort():

    <?php 
    $w3r_array = array("x" => "html", "y" => "xhtml", "z" => "xml");  
    krsort($w3r_array);  
    print_r($w3r_array);
    ?>
    

    Output :
    Array ( [x] => html [y] => xhtml [z] => xml )

    1. krsort(): It is an array function which is used to sorts an array and is used to sort an array by key in reverse order.

    Example of krsort():

    <?php 
    $w3r_array = array("x" => "html", "y" => "xhtml", "z" => "xml");  
    krsort($w3r_array);  
    print_r($w3r_array);
    ?>
    

    Output :
    Array ( [z] => xml [y] => xhtml [x] => html )

    1. uksort(): It is an array function which is used to sorts an array by the element keys using user defined comparison function.

    Example of uksort():

    <?php  
    function my_sort($x, $y)  
    {  
    if ($x == $y) return 0;  
    return ($x > $y) ? -1 : 1;  
    }  
    $people = array("10" => "javascript",  
    "20" => "php", "60" => "vbscript",  
    "40" => "jsp");  
    uksort($people, "my_sort");  
    print_r ($people);
    ?>
    

    Output :
    Array ( [60] => vbscript [40] => jsp [20] => php [10] => javascript )

 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: