Re: Foreach question

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

 



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.

--
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