Re: Foreach question

From: Date: Tue, 05 Jul 2011 14:56:13 +0000
Subject: Re: Foreach question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-313889@lists.php.net to get a copy of this message
On 11-07-05 09:40 AM, Dajka Tamas wrote:
foreach ( $cats as&$c ) {
                echo $c['id'];
                if ( $c['id']<  5 ) {
                               $c['id']++;
                               $cats[] = $c;
                }
}
Given that you seem to want the above functionality obtained when more than one element exists in the input array... the simplest way (I can bother to think up) to achieve what you want with little extra work is to do the following: <?php $cats['_control_'] = null; foreach ( $cats as &$c ) {
    if( $c === null )
    {
        continue;
    }
    echo $c['id'];
    if ( $c['id'] < 5 )
    {
        $c['id']++;
        $cats[] = $c;
    }
} unset( $cats['_control_'] ); ?> 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.

« previous php.general (#313889) next »