Re: Foreach question
| From: | Robert Cummings | Date: | Tue, 05 Jul 2011 14:05:59 +0000 |
| Subject: | Re: Foreach question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-313874@lists.php.net to get a copy of this message | ||
On 11-07-05 09:40 AM, Dajka Tamas wrote:
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 ) {That's a bizarre loop... you're feeding references to elements of the array back into the array over which the loop is iterating. If you REALLY want to do what you are doing, then do the following: <?php foreach( array_keys( $cats ) as $key ) {echo $c['id'];if ( $c['id']< 5 ) {$c['id']++;$cats[] = $c;}}
$c = &$cats[$key];
echo $c['id'];
if( $c['id'] < 5 )
{
$c['id']++;
$cats[] = $c;
}
}
?>
Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.