Re: [PHP] Rewind foreach loop



Jeffery Fernandez wrote:
Hi all,

Is it possible to rewind a foreach loop? eg:


$numbers = array(0,1,2,3,4,5,6,7,8,9,10);

foreach ($numbers as $index => $value)
{
if ($value == 5)
{
prev($numbers);
}
echo "Value: $value" . PHP_EOL;
}

this might give you an[other] idea ... note it's
an infinite loop - watch out for those.

do {
foreach (array(0,1,2,3,4,5,6,7,8,9,10) as $k => $v) {
if ($v == 5) continue 2;
echo $v, PHP_EOL;
}
} while (true);


The above doesn't seem to work. In one of my scenarios, when I encounter and
error in a foreach loop, I need the ability to rewind the array pointer by
one. How can I achieve this?

regards,
Jeffery
.



Relevant Pages

  • Re: [PHP] Rewind foreach loop
    ... Jeffery Fernandez wrote: ... In one of my scenarios, when I encounter and error in a foreach loop, I need the ability to rewind the array pointer by one. ... "Some men are born to greatness, some achieve greatness, ...
    (php.general)
  • Re: [PHP] Rewind foreach loop
    ... Jeffery Fernandez wrote: ... In one of my scenarios, when I encounter and error in a foreach loop, I need the ability to rewind the array pointer by one. ... Postgresql & php tutorials ...
    (php.general)
  • Re: [PHP] Rewind foreach loop
    ... Is it possible to rewind a foreach loop? ... to rewind the array pointer and continue with the loop. ... Victoria - 3136 ...
    (php.general)
  • Re: [PHP] Rewind foreach loop
    ... > and error in a foreach loop, I need the ability to rewind the array ... rewind the array pointer and continue with the loop. ... I would think that if you rewound the array pointer as above, you'd simply end up in an infinite loop, as you'd keep hitting the condition that triggered the rewind. ...
    (php.general)
  • Thanks a Ton..Mr Gunnar..
    ... Regards ... Sunish Kapoor ... Could anyone please help with Line Extraction and Emailing ... > Then you need an inner foreach loop inside the current foreach loop in ...
    (perl.beginners)