Re: Rewind foreach loop
| From: | Robert Cummings | Date: | Fri, 30 Nov 2007 19:49:52 +0000 |
| Subject: | Re: Rewind foreach loop | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-265314@lists.php.net to get a copy of this message | ||
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!
...........................................................