You are here: Re: Array pointer for internal(?) functions « PHP Programming Language « IT news, forums, messages
Re: Array pointer for internal(?) functions

Posted by Oli Filth on 09/28/34 11:37

André Hänsel said the following on 15/01/2006 23:50:
> Chung Leong wrote:
>> André Hänsel wrote:
>>> Hi!
>>>
>>> This:
>>>
>>> function foo()
>>> {
>>> $a = array('a' => 'b', 'c' => 'd');
>>> return array_keys($a);
>>> }
>>> while ($a = each(foo()))
>>> {
>>> echo 'dummy';
>>> }
>>>
>>> produces an infinite loop. Probably since the result of array_key
>>> doesn't support an array pointer or so, I don't know how this is
>>> internally implemented.
>>>
>>> Shouldn't this be considered as a bug? Shouldn't be the resulst of
>>> array_key walkable with each?
>> It's a bug alright--yours. foo() will return a fresh array each time,
>> so the condition is always true.
>
> Oh, I see.
>
> Is there any solution that supports the "while ($a = each(foo()))"
> construction?
> Caching the result in a static variable?
>

I guess you could do:

function &foo()
{
static $s = null;
if (null == $s)
{
$a = array('a' => 'b', 'c' => 'd');
$s = array_keys($a);
}
return $s;
}

while ($a = each(foo()))
{
echo 'dummy';
}

Although quite why you would want to do something so perverse, I've no
idea...

You'd be better off writing a custom storage class that implements the
Iterator interface.


--
Oli

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация