note 34552 added to control-structures.foreach

From: Date: Wed, 30 Jul 2003 15:55:53 +0000
Subject: note 34552 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-53018@lists.php.net to get a copy of this message
If you need to retain any modifications to the values of the array while looping through it, I've found this to be a good method: Instead of this: foreach ($arr as $a) { $a['name'] = "Bob"; } Use this: for ($i=0; $i < count($arr); $i++) { $a = &$arr[$i]; $a['name'] = "Bob"; } ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34552 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34552&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34552&report=yes

« previous php.notes (#53018) next »