Re: [PHP] Rewind foreach loop

2007-11-30 Thread Robert Cummings
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!
...

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



Re: [PHP] Rewind foreach loop

2007-11-30 Thread Robert Cummings
On Fri, 2007-11-30 at 09:51 -0800, Jim Lucas 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?
  
  regards,
  Jeffery
 
 No, you can't rewind the array.  Foreach makes a copy of the array, and works 
 off that copy.
 
 You might need to look into using do...while() instead
 
 Something thing like this should work.
 
 
 ?php
 
 $row = current($your_array);  Gives you the first element in $your_array
 
 do {
 
 ...
 work with your $row...
 determine if their is an error
 ...
 
   if ( $error ) {
   # Watch out for infinite loop
   # maybe have a counter that increments each time it rewinds 
 $your_array
   #and have it stop at 10 loops or something.
   prev($your_array);
   }
 } while( $row = next($your_array) );
 
 ?

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 );
}

?

Cheers,
Rob.
-- 
...
SwarmBuy.com - http://www.swarmbuy.com

Leveraging the buying power of the masses!
...

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



Re: [PHP] Rewind foreach loop

2007-11-30 Thread Jim Lucas

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?


regards,
Jeffery


No, you can't rewind the array.  Foreach makes a copy of the array, and works 
off that copy.

You might need to look into using do...while() instead

Something thing like this should work.


?php

$row = current($your_array);  Gives you the first element in $your_array

do {

...
work with your $row...
determine if their is an error
...

if ( $error ) {
# Watch out for infinite loop
# maybe have a counter that increments each time it rewinds 
$your_array
#and have it stop at 10 loops or something.
prev($your_array);
}
} while( $row = next($your_array) );

?


This way, you are working on the only copy of $your_array

--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

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



Re: [PHP] Rewind foreach loop

2007-11-30 Thread Jochem Maas
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

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



Re: [PHP] Rewind foreach loop

2007-11-29 Thread Jeffery Fernandez
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

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



Re: [PHP] Rewind foreach loop

2007-11-29 Thread Casey

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

Untested, but should work.

On Nov 29, 2007, at 7:13 PM, Chris [EMAIL PROTECTED] 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



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



Re: [PHP] Rewind foreach loop

2007-11-29 Thread Steve Edberg

At 2:11 PM +1100 11/30/07, Jeffery Fernandez wrote:

On Fri, 30 Nov 2007 02:01:47 pm Chris [EMAIL PROTECTED] 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.



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. So I'm assuming you have some 
other test in there and this is just a stripped down example.


If you're using a one-dimensional array, as opposed to a 
multidimensional and/or associative array, you can do (untested):


$Count = count($numbers);

for ($i=0; $i$Count; $i++) {
$value = $numbers[$i];
if ($value == 5  some_other_test()) {
$value = $numbers[--$i];
}
echo Value: $value . PHP_EOL;
}

Wouldn't be much more complex to extend to a multidimensional array 
with an integer index. If you were using an associative array with a 
string index, you'd probably have to do something like


$NotJustNumbers = array('a'='slurm', 'b'='fry', 'c'='leela');
$Keys = array_keys($NotJustNumbers);
$Count = count($Keys);

for ($i=0; $i$Count; $i++) {
$value = $NotJustNumbers[$Keys[$i]];
if ($value == 5  some_other_test()) {
$value = $NotJustNumbers[$Keys[--$i]];
}
echo Value: $value . PHP_EOL;
}


- steve

--
+--- my people are the people of the dessert, ---+
| Steve Edberghttp://pgfsun.ucdavis.edu/ |
| UC Davis Genome Center[EMAIL PROTECTED] |
| Bioinformatics programming/database/sysadmin (530)754-9127 |
+ said t e lawrence, picking up his fork +

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



Re: [PHP] Rewind foreach loop

2007-11-29 Thread Chris

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/

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



Re: [PHP] Rewind foreach loop

2007-11-29 Thread Jeffery Fernandez
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.


 Of course that assumes that the value you're looking for isn't the first
 element :)

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

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



Re: [PHP] Rewind foreach loop

2007-11-29 Thread Chris

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



Re: [PHP] Rewind foreach loop

2007-11-29 Thread Jeffery Fernandez
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

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