Re: Array mysteries

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

 



On Sun, March 11, 2007 12:02 pm, 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...

If you are calling this function a ridiculous number of times, you
could use a static variable.

function convert_from_weekday($weekday){
  static $weeks = '';
  if ($weeks === '') $weeks = array('Sonntag', 'Montag', ...);
}

It's kind of a shame that PHP won't let you just initialize a static
to an array, but there it is.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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