note 44875 deleted from control-structures.foreach by tomsommer

From: Date: Wed, 18 Aug 2004 19:30:25 +0000
Subject: note 44875 deleted from control-structures.foreach by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-74904@lists.php.net to get a copy of this message
Note Submitter: court shrock ---- 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.

« previous php.notes (#74904) next »