Re: [PHP] Rewind foreach loop



$keys = array_values($array);
for ($i=0; $i<count($keys); $i++) {
if ($keys[$i] == 5)
$i -= 2;
}

Untested, but should work.

On Nov 29, 2007, at 7:13 PM, Chris <dmagick@xxxxxxxxx> 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 ...

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

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

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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

.



Relevant Pages

  • Re: Help With Complicated SP (for me any way)
    ... Please post DDL, so that people do not have to guess what the keys, ... constraints, Declarative Referential Integrity, datatypes, etc. in your ... that loop thru things one at a time in SQL. ... CREATE PROCEDURE Bookings ...
    (microsoft.public.sqlserver.programming)
  • Re: Variation on prisoners dilemma
    ... jail cells in a jailhouse in some small town in Kansas. ... locked up thieves plead to be released so they to can take shelter. ... suddenly come to learn that the jailer tossed the wrong set of keys to ... the keys to diametrically opposite parts of the loop. ...
    (rec.puzzles)
  • Re: Enumerating in separate threads--pointless because the thread is blocked? (.NET 2.0)
    ... ManagementObjectSearcher mos = new ManagementObjectSearcher( ... ProcessThisRecord(mbo); ... then as I enter the foreach loop, whatever thread is executing the loop ... the thread is blocked as soon as I enter the foreach loop. ...
    (microsoft.public.win32.programmer.wmi)
  • Enumerating in separate threads--pointless because the thread is blocked? (.NET 2.0)
    ... ManagementObjectSearcher mos = new ManagementObjectSearcher(...query... ... ProcessThisRecord(mbo); ... as I enter the foreach loop, whatever thread is executing the loop then ... the thread is blocked as soon as I enter the foreach loop. ...
    (microsoft.public.win32.programmer.wmi)
  • Re: sorting and resorting hash
    ... There's no reason to be adding values to an array if you're only using ... Just build the hash directly. ... of the entire %rms hash each time through the loop. ... Why are you creating a whole new array to just store the keys of the hash ...
    (comp.lang.perl.misc)