Re: [PHP] Rewind foreach loop



I think the best option for me is to refactorise my code a bit to cater to my
situation. Thanks all for your help.

Jeffery

On Fri, 30 Nov 2007 02:32:11 pm Jeffery Fernandez wrote:
On Fri, 30 Nov 2007 02:13:52 pm Chris wrote:
Jeffery Fernandez wrote:
On Fri, 30 Nov 2007 02:01:47 pm Chris wrote:
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;
}

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?

echo $numbers[$index-1] . PHP_EOL;

That will only give me the value of the previous index. What I want is
to rewind the array pointer and continue with the loop.

and you're going to be in an endless loop then.. because each time it
gets rewound, it gets the same key again and rewinds and ...

No, I am only rewinding if there is an error. Then I have the script
auto-learning from the error, fix a config file and then want to go back to
the array pointer to re-execute the process.

You could do it with a for or while loop probably.

Thats what I am looking at now.

What are you trying to achieve? Maybe there's an alternative.

As mentioned above.

cheers,
Jeffery

--
Postgresql & php tutorials
http://www.designmagick.com/

--
Internet Vision Technologies
Level 1, 520 Dorset Road
Croydon
Victoria - 3136
Australia
web: http://www.ivt.com.au
phone: +61 3 9723 9399
fax: +61 3 9723 4899



--
Internet Vision Technologies
Level 1, 520 Dorset Road
Croydon
Victoria - 3136
Australia
web: http://www.ivt.com.au
phone: +61 3 9723 9399
fax: +61 3 9723 4899
.



Relevant Pages

  • 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)
  • Re: [PHP] Rewind foreach loop
    ... 30 Nov 2007 02:01:47 pm Chris wrote: ... Is it possible to rewind a foreach loop? ... rewind the array pointer and continue with the loop. ...
    (php.general)
  • Rewind foreach loop
    ... Is it possible to rewind a foreach loop? ... Level 1, 520 Dorset Road ... Victoria - 3136 ...
    (php.general)
  • Re: [PHP] Rewind foreach loop
    ... Is it possible to rewind a foreach loop? ... rewind the array pointer and continue with the loop. ...
    (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. ... "Some men are born to greatness, some achieve greatness, ...
    (php.general)