Re: Rewind foreach loop

From: Date: Fri, 30 Nov 2007 03:01:47 +0000
Subject: Re: Rewind foreach loop
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-265277@lists.php.net to get a copy of this message
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; Of course that assumes that the value you're looking for isn't the first element :) -- Postgresql & php tutorials http://www.designmagick.com/

« previous php.general (#265277) next »