RE: [PHP] using a counter in a foreach loop

2002-03-26 Thread Jon Haworth

Hi Erik,

> I have a foreach loop, where I execute some 
> commands for each element in a certain array.  
> One thing I would like to add to this loop is a 
> counter, so that on each iteration of the loop 
> I have a next higher number.  The following does 
> not work:
> 
> foreach ($months) {
>$i = 1;
>// do some things
>$i++;
> }
> 
> because in each new iteration of the loop, $i 
> is reset to 1.  Is there a way that I can 
> achieve this effect?  Thanks for any advice,

Put the $i = 1 outside the loop.

$i = 1;
foreach ($months) {
   // do some things
   $i++;
}

Incidentally, I *really* hope you're not planning on doing:
$i = 1;
foreach ($months) {
  echo "month ". $i. " is ". $months[$i]. "";
  $i++;
}

if you are, I suggest you play around with
foreach ($months as $currentMonth) {

and see where it gets you :-)

Cheers
Jon



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




Re: [PHP] using a counter in a foreach loop

2002-03-26 Thread Jan Rademaker

On Tue, 26 Mar 2002, Erik Price wrote:

> I have a pretty basic question and was wondering if someone could point 
> me in the right direction:
> 
> I have a foreach loop, where I execute some commands for each element in 
> a certain array.  One thing I would like to add to this loop is a 
> counter, so that on each iteration of the loop I have a next higher 
> number.  The following does not work:
> 
> foreach ($months) {
>$i = 1;
>// do some things
>$i++;
> }
> 
> because in each new iteration of the loop, $i is reset to 1.  Is there a 
> way that I can achieve this effect?  Thanks for any advice,

well... put $i = 1; outside your loop...?

$i = 1;
foreach ($months) {
// do some things
$i++;
}

-- 
Jan Rademaker <[EMAIL PROTECTED]>
http://www.ottobak.com



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




Re: [PHP] using a counter in a foreach loop

2002-03-26 Thread Erik Price

What an embarrassing oversight.  I'm sorry, I figured this one out.  You 
don't define the $i counter variable in the loop (DUH).

$i = 1;
foreach ($months) {
   // do some things
   $i++;
}

Thanks to all those who may respond before reading this


Erik


On Tuesday, March 26, 2002, at 09:49  AM, Erik Price wrote:

> I have a pretty basic question and was wondering if someone could point 
> me in the right direction:
>
> I have a foreach loop, where I execute some commands for each element 
> in a certain array.  One thing I would like to add to this loop is a 
> counter, so that on each iteration of the loop I have a next higher 
> number.  The following does not work:
>
> foreach ($months) {
>   $i = 1;
>   // do some things
>   $i++;
> }
>
> because in each new iteration of the loop, $i is reset to 1.  Is there 
> a way that I can achieve this effect?  Thanks for any advice,
>
>
> Erik






Erik Price
Web Developer Temp
Media Lab, H.H. Brown
[EMAIL PROTECTED]
>
>
> -- 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] using a counter in a foreach loop

2002-03-26 Thread Matt Schroebel

$i = 1;
foreach ($months) {
   // do some things
   $i++;
}
> -Original Message-
> From: Erik Price [mailto:[EMAIL PROTECTED]] 
> Sent: Tuesday, March 26, 2002 9:49 AM

> 
> I have a foreach loop, where I execute some commands for each 
> element in 
> a certain array.  One thing I would like to add to this loop is a 
> counter, so that on each iteration of the loop I have a next higher 
> number.  The following does not work:
> 
> foreach ($months) {
>$i = 1;
>// do some things
>$i++;
> }
> 

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