note 97945 modified in control-structures.foreach by danbrown
| From: | danbrown@php.net | Date: | Tue, 18 May 2010 15:20:28 +0000 |
| Subject: | note 97945 modified in control-structures.foreach by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169370@lists.php.net to get a copy of this message | ||
For those wishing to try the heresy of adding to an array while it is being processed in a foreach
loop, note that your added value will not be processed at the end of the loop!
<?php
$arr = array(1,2,3,4,5);
foreach($arr as $val) if($val==3) $arr[]=6; else echo $val;
?>
will show "1245" not "12456".
--was--
For those wishing to try the heresy of adding to an array while it is being processed in a foreach
loop, note that your added value will not be processed at the end of the loop!
$arr = array(1,2,3,4,5);
foreach($arr as $val) if($val==3) $arr[]=6; else echo $val;
will show "1245" not "12456".
http://php.net/manual/en/control-structures.foreach.php