note 98810 deleted from control-structures.foreach by danbrown
| From: | danbrown@php.net | Date: | Thu, 08 Jul 2010 22:21:26 +0000 |
| Subject: | note 98810 deleted from control-structures.foreach by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170806@lists.php.net to get a copy of this message | ||
Note Submitter: g_adams27 at hotmail dot com
----
Regarding John's comment (17-May-2010) about adding to an array while it is being processed, he
is correct - you will have to use "each" to accomplish that.
# This prints 1245, not 12456.
$arr = array(1,2,3,4,5);
foreach($arr as $val) if($val==3) $arr[]=6; else echo $val;
# This DOES print 12456
$arr = array(1,2,3,4,5);
while(list(,$val) = each($arr)) if($val==3) $arr[]=6; else echo $val;