Re: Array mysteries

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 2007-03-11 at 21:41 +0100, Satyam wrote:
> ----- Original Message ----- 
> From: "Edward Vermillion" <evermillion@xxxxxxxxxxxx>
> To: "tedd" <tedd@xxxxxxxxxxxx>
> Cc: "Tijnema !" <tijnema@xxxxxxxxx>; <php-general@xxxxxxxxxxxxx>
> Sent: Sunday, March 11, 2007 8:57 PM
> Subject: Re:  Array mysteries
> 
> 
> >
> > On Mar 11, 2007, at 1:59 PM, tedd wrote:
> >
> >> At 12:02 PM -0500 3/11/07, Edward Vermillion wrote:
> >>> On Mar 11, 2007, at 10:02 AM, tedd wrote:
> >>>
> >>>> At 3:05 PM +0100 3/11/07, Tijnema ! wrote:
> >>>>> On 3/11/07, tedd <<mailto:tedd@xxxxxxxxxxxx>tedd@xxxxxxxxxxxx>  wrote:
> >>>>>
> >>>>> At 10:05 AM +0100 3/11/07, Tijnema ! wrote:
> >>>>>>
> >>>>>> - You could define $wdays inside the function
> >>>>>> function convert_from_weekday ($weekday) {
> >>>>>> $wdays = array
> >>>>>>    (0 => "Sonntag"
> >>>>>>    ,1 => "Montag"
> >>>>>>    ,2 => "Dienstag"
> >>>>>>    ,3 => "Mittwoch"
> >>>>>>    ,4 => "Donnerstag"
> >>>>>>    ,5 => "Freitag"
> >>>>>>    ,6 => "Samstag"
> >>>>>>  );
> >>>>>>    return $wdays[$weekday];
> >>>>>>  }
> >>>>>> $day = convert_from_weekday(0) // $day = "Sonntag"
> >>>>>
> >>>>> Tijnema:
> >>>>>
> >>>>> That's also a shorter version of a simple switch statement.
> >>>>>
> >>>>> I haven't thought of, or seen, that before -- thanks.
> >>>>>
> >>>>> tedd
> >>>>>
> >>>>>
> >>>>> Yeah it is, but i just used moved his $wdays inside the function...
> >>>>>
> >>>>> but well, there are ofcourse a lot of other options, as date ("l") 
> >>>>> would also return the day of the month :)
> >>>>>
> >>>>> Tijnema
> >>>>
> >>>>
> >>>> It's the technique and not the specific data thing I was  addressing. 
> >>>> When I'm confronted with a case condition, I  typically use the switch 
> >>>> statement. But, your solution provided  me with another way to look at 
> >>>> that.
> >>>>
> >>>> Cheers,
> >>>>
> >>>> tedd
> >>>
> >>> But what's the cost of this in a loop, rebuilding the array each  time, 
> >>> as compared to a switch statement? Just another thought...
> >>>
> >>
> >>
> >>
> >> Ed
> >>
> >> It's just another way to look at a possible solution.
> >>
> >> As for the cost, what are we talking about? I doubt that a  "typical" 
> >> application would show any discernable difference in  execution times.
> >>
> >> One could test this easy enough by running both through 50k loops,  but 
> >> even then I doubt that the times would be that much different  -- but I 
> >> may be wrong, been there before.
> >>
> >> Cheers,
> >>
> >> tedd
> >
> > I don't know if there would be any difference either, which is why it  was 
> > a question.
> >
> > Although Larry's suggestion of making the array static is something I 
> > hadn't thought of.
> >
> > Overall it is an interesting concept to use an array instead of a  switch, 
> > and I do wonder at what point, if any, that the two would  start to 
> > diverge resource-wise.
> >
> > Would the array lookup be faster for a lesser-used option/key in a 
> > situation where there were quite a few options? (you wouldn't have to  go 
> > through the whole switch to get to the option at the end (?) or  would 
> > you? I have no idea how that all works internally)
> >
> Yes, you would.  It goes sequentially through each case:.  The array, on the 
> other hand, uses a hashing algorithm so it should be about even no matter 
> which option you pick.

Not quite. When the number of possible keys are small, a rote traversal
using language constructs such as if/elseif/else/case is likely to be
faster than incurring the overhead of the hash search. However, in
general the hash search will be faster. Having said that, the use of an
array to hold the key/value pairs produces a very succinct and readable
solution. Additionally, retrieval of such values from the database is
more easily implemented using the array methodology. For those not aware
of using static hash lookups with database results I've included an
example:

<?php

function convert_from_weekday( $weekday )
{
    static $days = null;

    if( $days === null )
    {
        $query =
            " SELECT "
            "    weekday_id "
            "    weekday_name "
            "FROM "
            "    weekday_table ";

        $days = array();
        if( $db->query( $query ) )
        {
            while( $db->nextRow() )
            {
                $days[$db->getField( 'weekday_id' )] =
                    $days[$db->getField( 'weekday_name' )];
            }
        }
    }

    if( isset( $days[$weekday] ) )
    {
        return $days[$weekday];
    }

    return null;
}

?>

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux