Re: [PHP] Rewind foreach loop



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