Re: Rewind foreach loop

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

 



On Fri, 2007-11-30 at 14:46 -0500, Robert Cummings wrote:
>
> This is dangerous use of the array functions. A problem occurs when you
> have a value that evaluates to false (such as the first entry in the
> example array :). In fact the only way to ensure you traverse the array
> properly is to use each() since it returns an array except when no more
> entries exist. Also you want to reset() the array before looping
> (normally anyways).
> 
> <?php
> 
> reset( $data );
> while( ($entry = each( $data )) )
> {
>     // ...
> 
>     if( $errorCondition )
>     {
>         prev( $data );
>         continue;
>     }
> 
>     next( $data );

Newbie bug!! Newbie bug!! each() advances the array pointer so don't do
a next() call :)

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

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