note 44875 added to control-structures.foreach
| From: | courtshrock at osu1 dot php dot net | Date: | Wed, 18 Aug 2004 17:47:20 +0000 |
| Subject: | note 44875 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-74893@lists.php.net to get a copy of this message | ||
as a followup, you can get around this "feature" by immediately repeating an assignment,
that will pull from addressable $arr that you are modifying:
<?php
$arr = array('a', 'b', 'c', 'd', ''); # notice
that the 5th value has to be defined
foreach ($arr as $k=>$v) {
$v = $arr[$k]; # <== this reassigns from addressible memory
echo "$v\n";
if ($v == 'b') $arr[4] = 'e';
if ($v == 'e') echo "found e\n";
}// foreach
echo '..done..'
?>
notice that I had to make sure that $arr[4] would be defined so that the loop would execute (by
modifying the $arr assignment)....meaning, you still can't change how many times the loop
executes by doing an assignment inside the loop, but at least you can reference changed values.
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44875
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44875&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44875&report=yes
Search -- http://master.php.net/manage/user-notes.php