note 56914 added to language.types.array

From: Date: Mon, 19 Sep 2005 13:14:27 +0000
Subject: note 56914 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-95407@lists.php.net to get a copy of this message
Regarding the previous comment, the fact that this code has no effect is perfectly expected: <?php foreach($arr as $value) { $value = 1; } ?> The reason that this doesn't work, is because each time that PHP goes through the loop, it _copies_ the value of the array element into $value. So if you assign a new value to the data in $value, it has no effect on the actual array, because you only changed the value of the copy that was put in $value. As was discovered in the previous post, the only way to get around this problem is to change the value in the original array. Hence, a typical foreach should instead look like this: <?php foreach($arr as $key => $value) { $arr[$key] = 1; } ?> ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56914 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56914&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56914&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56914&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56914&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56914&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56914&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56914&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56914&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95407) next »