Confusing pointers in PHP 4 and 5
| From: | Taco van den Broek | Date: | Tue, 08 Jun 2004 09:17:14 +0000 |
| Subject: | Confusing pointers in PHP 4 and 5 | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-10308@lists.php.net to get a copy of this message | ||
While venus is crossing the sun I'm trying to model a tree using two arrays; one containing the structure of the tree and the other containing the data of the nodes.
The second array also contains pointers to 'subtrees' of the first array. Anyway, I discovered a strange behavior when I dumped the tree before and after making the pointers.
Example:
<?php
$tree = array( 1 => array( 2 => 'foo' ) );
var_dump($tree);
$b = &$tree[1];
var_dump($tree);
?>
This code will output the following:
array(1) {
[1]=>
array(1) {
[2]=>
string(3) "foo"
}
}
array(1) {
[1]=>
&array(1) {
[2]=>
string(3) "foo"
}
}
As you can see the original tree now contains a pointer, how can that be, I didn't change the original array!?
So what I'd like to know is: Is this intended behavior, and if so: why does it work like this and would it make some operations impossible?
Taco