Re: Foreach question
| From: | Louis Huppenbauer | Date: | Tue, 05 Jul 2011 15:46:38 +0000 |
| Subject: | Re: Foreach question | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-313900@lists.php.net to get a copy of this message | ||
Just use count($arr) in your for-header, as it get's executed again
for each loop.
<?php
$arr = array(array('id'=>1), array('id'=>2));
for($i=0;$i<count($arr);$i++) {
echo $arr[$i]['id'];
if($i < 6) {
$arr[] = array('id' => $arr[$i]['id']+1);
}
}
?>
2011/7/5 Robert Cummings <robert@interjinn.com>:
> On 11-07-05 10:48 AM, Dajka Tamás wrote:
>>
>> Thanks, that was interesting :) I think I got one step further in
>> understanding PHP :)
>>
>> BTW, I've changed the loop to 'for' and it's working well :)
>
> Can you show us your for loop? I'm not immediately sure how you use a for
> loop to traverse a growing number of entries in an array without either
> updating the extents of the traversal or using for( ; ; ) which is the same
> as while( 1 ). Or are you now using the low level array traversal functions
> like reset() and next()?
>
> 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
>
>