Bug #15273: Reference to array breaks
| From: | mvl at nikhef dot nl | Date: | Tue, 29 Jan 2002 10:21:16 +0000 |
| Subject: | Bug #15273: Reference to array breaks | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-78433@lists.php.net to get a copy of this message | ||
From: mvl@nikhef.nl
Operating system: Linux
PHP version: 4.1.1
PHP Bug Type: Arrays related
Bug description: Reference to array breaks
Hi,
Below you will find a piece of code which is intended to make two
two-dimensional arrays: $aa and $bb. In fact, both arrays should be the
same, as far as I can see. It seems however that the unset($b_nw) is
crucial.
As far as I understood from the documentation, an assignement with an
arrray on the right-hand side should perfrom a copy of the array. My
assumption is that the same should be true with a reference to an array,
because there is no way to distinguish between them. So please have a look
at my code and tell me when you need more information, or if I
misunderstood something.
The code:
<?php
$a=array(0);
$b=array(0);
$aa=array(&$a);
$bb=array(&$b);
for ($i=1; $i<3; $i++) {
$a_nw=$a;
$a_nw[]=$i;
$aa[]=&$a_nw;
$b_nw=$b;
$b_nw[]=$i;
$bb[]=&$b_nw;
unset($b_nw);
}
echo "<b>aa: </b>";
print_r($aa);
echo "<br>\n";
echo "<b>bb: </b>";
print_r($bb);
echo "<br>\n";
?>
The output:
aa: Array ( [0] => Array ( [0] => 0 ) [1] => Array ( [0] => 0 [1] => 2 )
[2] => Array ( [0] => 0 [1] => 2 ) )
bb: Array ( [0] => Array ( [0] => 0 ) [1] => Array ( [0] => 0 [1] => 1 )
[2] => Array ( [0] => 0 [1] => 2 ) )
Note that $aa[1] != $bb[1], which is unexepected ($bb[1] is the correct
value, I think).
Thanks,
Marco.
--
Edit bug report at: http://bugs.php.net/?id=15273&edit=1