RE: Re[2]: [PHP] foreach vs. while(list() = each())

From: Date: Thu, 12 Apr 2001 15:33:23 +0000
Subject: RE: Re[2]: [PHP] foreach vs. while(list() = each())
Groups: php.general 
Request: Send a blank email to php-general+get-48327@lists.php.net to get a copy of this message
not for the same array it can't, the pointer in the first foreach is moved by the second one try: $array = array("a", "b", "c"); foreach($array as $val1) { foreach($array as $val2) echo "$val1:$val2<br>"; } it gives a:a a:b a:c then stops, the manual says that the foreach creates it's own pointer which it obviously doesn't. I can't remember what I was trying to do when I found this, it might have been writing a recursive function Tim Ward Senior Systems Engineer Please refer to the following disclaimer in respect of this message: http://www.stivesdirect.com/e-mail-disclaimer.html > -----Original Message----- > From: Maxim Derkachev [mailto:kot@books.ru] > Sent: 12 April 2001 16:23 > To: Tim Ward > Cc: php-general@lists.php.net > Subject: Re[2]: [PHP] foreach vs. while(list() = each()) > > > Hello Tim, > > Thursday, April 12, 2001, 1:43:53 PM, you wrote: > > TW> you can't nest foreach as you should be able to. Ecah > foreach is supposed to > TW> have it's own pointer in the array, but it doesn't ... > this is a known bug. > > What do you mean? Foreach() can be nested, and it works perfect. > > <?php > $a = array (array (1, 2, 3), array(4,5,6), array(7,8,9)); > foreach ($a as $k => $first) { > print "$k:\n"; > foreach ($first as $second) { > print "\t$second\n"; > } > } > ?> > > output: > 0: > 1 > 2 > 3 > 1: > 4 > 5 > 6 > 2: > 7 > 8 > 9 > > > > > -- > Best regards, > Maxim Derkachev mailto:kot@books.ru > Symbol-Plus Publishing Ltd. > phone: +7 (812) 324-53-53 > http://www.Books.Ru -- All Books of Russia >   > >

« previous php.general (#48327) next »