Re: [PHP] Rewind foreach loop
- From: robert@xxxxxxxxxxxxx (Robert Cummings)
- Date: Fri, 30 Nov 2007 14:49:52 -0500
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!
............................................................
.
- References:
- Rewind foreach loop
- From: Jeffery Fernandez
- Re: [PHP] Rewind foreach loop
- From: Jim Lucas
- Re: [PHP] Rewind foreach loop
- From: Robert Cummings
- Rewind foreach loop
- Prev by Date: Re: [PHP] Rewind foreach loop
- Next by Date: RE: [PHP] including parenthesis, space and dashes in a phone number
- Previous by thread: Re: [PHP] Rewind foreach loop
- Next by thread: PDT Zend Debugger
- Index(es):