note 82486 added to function.each
| From: | php at omit dot ianco dot co dot uk | Date: | Sun, 13 Apr 2008 13:10:55 +0000 |
| Subject: | note 82486 added to function.each | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138264@lists.php.net to get a copy of this message | ||
I wanted to be able to add to an array while looping through it. foreach does not allow this because
it is using a secret copy of the array. each makes this possible (tested on PHP 4).
<?php
$shopping_list = array('oysters', 'caviare');
reset ($shopping_list);
while (list($key, $value) = each ($shopping_list)) {
if ($value == 'oysters') $shopping_list[] = 'champagne';
elseif ($value == 'champagne') $shopping_list[] = 'ice';
}
print_r($shopping_list);
// Array ( [0] => oysters [1] => caviare [2] => champagne [3] => ice )
?>
----
Server IP: 87.124.64.96
Probable Submitter: 84.92.139.196
----
Manual Page -- http://www.php.net/manual/en/function.each.php
Edit -- https://master.php.net/note/edit/82486
Del: integrated -- https://master.php.net/note/delete/82486/integrated
Del: useless -- https://master.php.net/note/delete/82486/useless
Del: bad code -- https://master.php.net/note/delete/82486/bad+code
Del: spam -- https://master.php.net/note/delete/82486/spam
Del: non-english -- https://master.php.net/note/delete/82486/non-english
Del: in docs -- https://master.php.net/note/delete/82486/in+docs
Del: other reasons-- https://master.php.net/note/delete/82486
Reject -- https://master.php.net/note/reject/82486
Search -- https://master.php.net/manage/user-notes.php