note 71854 added to control-structures.foreach
| From: | simplex at osu1 dot php dot net | Date: | Tue, 19 Dec 2006 20:12:18 +0000 |
| Subject: | note 71854 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120776@lists.php.net to get a copy of this message | ||
"As of PHP 5, you can easily modify array's elements by preceding $value with &. This
will assign reference instead of copying the value."
There are cases where array_walk or array_map are inadequate (conditional required) or you're
just too lazy to write a function and pass values to it for use with array_map...
My solution to foreach for php 4 and 5 to modify values of an array directly:
<?php
$testarr = array("a" => 1, "b" => 2, "c" => 3, "d"
=> 4);
$testarr_keys = array_keys($testarr);
$testarr_values = array_values($testarr);
for ($i = 0; $i <= count($testarr) - 1; $i++) {
$testarr[$testarr_keys[$i]] = $testarr_values[$i] * 2;
}
print_r($testarr);
?>
----
Server IP: 216.235.15.211
Probable Submitter: 142.166.242.46
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- https://master.php.net/note/edit/71854
Del: integrated -- https://master.php.net/note/delete/71854/integrated
Del: useless -- https://master.php.net/note/delete/71854/useless
Del: bad code -- https://master.php.net/note/delete/71854/bad+code
Del: spam -- https://master.php.net/note/delete/71854/spam
Del: non-english -- https://master.php.net/note/delete/71854/non-english
Del: in docs -- https://master.php.net/note/delete/71854/in+docs
Del: other reasons-- https://master.php.net/note/delete/71854
Reject -- https://master.php.net/note/reject/71854
Search -- https://master.php.net/manage/user-notes.php