Saturday, March 27, 2010

Sorting functions in php sort(), asort() ksort() -- difference between sort() asort() arsort() rsort()

Sorting functions in php sort(), asort() ksort()
difference between sort() asort() arsort() rsort()

<?php

    $fruits = array("lemon", "orange", "banana", "apple");
    echo "The Original Array is:<br>";
    foreach ($fruits as $key => $val)
    {
        echo "fruits[" . $key . "] = " . $val . "<br>";
    }

    echo "<br>The Array After sort() is:<br>";
    sort($fruits);
    /*
        sort()
        This function sorts an array. Elements will be arranged
        from lowest to highest when this function has completed.

    */
    foreach ($fruits as $key => $val)
    {
        echo "fruits[" . $key . "] = " . $val . "<br>";
    }
    /*
        ----------------------------
OUTPUT---------------------
        fruits[0] = apple
        fruits[1] = banana
        fruits[2] = lemon
        fruits[3] = orange
        -------------------------------------------------------
    */

    /*
        asort()
        This function sorts an array such that array indices
        maintain their correlation with the array elements they are
        associated with. This is used mainly when sorting
        associative arrays where the actual element order is
        significant.
    */
    echo "<br>The Array After asort() is:<br>";
    $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
    asort($fruits);
    foreach ($fruits as $key => $val)
    {
        echo "$key = $val<br>";
    }
    /*
        --------------------OUTPUT------------------------
        c = apple
        b = banana
        d = lemon
        a = orange
        --------------------------------------------------

    */
   
    /*
        ksort()
    Sorts an array by key, maintaining key to data
    correlations. This is useful mainly for associative arrays.
    */
    echo "<br>The Array After ksort() is:<br>";
    $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
    ksort($fruits);
    foreach ($fruits as $key => $val)
    {
        echo "$key = $val<br>";
    }
    /*
    --------------------OUTPUT----------------------------
    a = orange
    b = banana
    c = apple
    d = lemon
    ------------------------------------------------------
    */

    echo "<br><br><br><br>Revese Sort";
    $input = array ("Pinaple", "Orange", "Banana", "Mango", "Apple","Strawberry");
    $input1=$input;
    $input2=$input;
    $input3=$input;
    echo "<br>-----Original Array---------<br>";
    while (list ($key, $val) = each ($input))
    {
        echo "$key -> $val <br>";
    }
    sort($input1);
    echo "<br>-----After sort---------<br>";
    while (list ($key, $val) = each ($input1))
    {
        echo "$key -> $val <br>";
    }

    rsort($input2);
    echo "<br>-----After rsort---------<br>";
    while (list ($key, $val) = each ($input2))
    {
        echo "$key -> $val <br>";
    }
    arsort($input3);
    echo "<br>-----After rsort---------<br>";
    while (list ($key, $val) = each ($input3))
    {
        echo "$key -> $val <br>";
    }
?>


--
******************************************************
http://www.venkatmails.blogspot.com/

We are richer when we give and poorer when we keep!
-------------------------------------------------
By the next 10 months, our earth will become 3 degrees hotter than its now .
Our himalayan glaciers are melting at a rapid rate. So all of you lend your
hands to fight global warming -Plant more trees .Don't waste water
--Don't use or burn plastic. - Control paper use.
-------------------------------------------------
"The fastest way to succeed is to double your failure rate." --Thomas J. Watson, Founder of IBM
-------------------------------------------------
A lion wakes up everyday and starts running after a deer to survive.
A deer wakes up everyday and starts running from a lion to survive.
So the point is that it does not matter whether you are a lion or a deer.
You just have to keep running.

http://www.venkatmails.blogspot.com/
******************************************************

No comments:

Post a Comment