Re: using a counter in a foreach loop

From: Date: Tue, 26 Mar 2002 14:53:14 +0000
Subject: Re: using a counter in a foreach loop
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-90191@lists.php.net to get a copy of this message
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 <janr@486.yi.org> http://www.ottobak.com

« previous php.general (#90191) next »