Re: Foreach question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxx>:
> 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
>
>

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




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux