Re: Foreach question

From: Date: Tue, 05 Jul 2011 13:45:36 +0000
Subject: Re: Foreach question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-313872@lists.php.net to get a copy of this message
Hi there I think that foreach in your first example just knowns that this should be the last loop (as the array only contains 1 element at start) and so stops there. In your 2nd example however the first loop isn't the last, so the array get's checked again, and now there's another element, so... I think that's more or less normal behaviour. Sincerely yours Louis 2011/7/5 Dajka Tamas <viper@vipernet.hu>: > Hi all, > > > > I've bumped into an interesting thing with foreach. I really don't know, if > this is normal working, or why it is, so I got curious. > > > > The script: > > > > foreach ( $cats as &$c ) { > >               echo $c['id']; > >               if ( $c['id'] < 5 ) { > >                              $c['id']++; > >                              $cats[] = $c; > >               } > > } > > > > Input 1: > > > > $cats = array( array( 'id' => 1 ) ); > > > > Output 1: > > > > 1 > > > > Input 2: > > > > $cats = array( array( 'id' => 1 ), array( 'id' => 2 ) ); > > > > Output 2: > > > > 122334455 > > > > > > Why is this? Is this normal behaviour? > > > > > > Thanks, > > > >               Tamas > >

« previous php.general (#313872) next »