|
Posted by Chung Leong on 12/11/05 19:23
Chameleon wrote:
> It is a php bug or a my bug?
It's your bug I suspect. The problem can't be reproduced with the code
as given. If you move the var_dump($a) to the end, then you see it.
<?
$a = array(array(1, 2), array(2, 3), array(4, 5));
$b = array(6, 7, 8);
foreach($a as & $v)
$v = array_merge($v, $b);
foreach($a as $v)
var_dump($v);
var_dump($a);
?>
The second foreach() is setting the value of $v to each element in $a.
At this point, $v is still pointing to the last element in $a,
resulting from the first foreach(). The last element in $a is thus is
set repeatedly to all elements preceding it.
It's a good idea to unset() references when they're no longer needed to
avoid unintended modification later on.
Navigation:
[Reply to this message]
|