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/
******************************************************

Javascript focus() , Text field focus. focus().----> Alternative code to focus()

Javascript focus()
Text field focus. focus().
Alternative code to focus()
Under IE, when an error occurs the cursor returns to the field with the error. Under Firefox, the cursor stays in the next (tabbed to) field. Is there a way to code this validation so that it would work for both??

Program:
<html>
<head>
<script language='javascript'>
function check()
{
    alert('hii');
    //document.joe.burns.focus(); This is valid for only internet explorer. And will not work on Firefox
    document.getElementById("
burns").focus();
    setTimeout("document.getElementById('burns').focus();",0);
}
function check2()
{
var letters2 = document.joe.tammy.value.length +1;
if (letters2 <= 4)
{document.joe.tammy.focus()}
else
{document.joe.chloe.focus()}
}

</script>
<body>
<FORM NAME="joe">
One:<INPUT TYPE="text" name="burns" id="burns" size="10" onchange="check()"><BR>
Two:<INPUT TYPE="text" name="tammy" size="10" onchange="check2()"><BR>
<INPUT TYPE="submit" VALUE="Click to Send" NAME="go">
</FORM>
</body>
</html>


--
******************************************************
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/
******************************************************