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]. "<br>"; $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