View Single Post

  #4 (permalink)  
Old 11-16-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Tree-like structure in PHP?

Paul van Haren wrote:
> I'm trying to use arrays to implement a sort of data tree. For the code to
> work, it is essential that the nodes can be edited after they have become
> part of the tree. That would require that the array elements are pushed
> "by reference" rather than "by value".
>
> <?php
>
> $arr1 = array ();
> $arr2 = array ();
>
> array_push ($arr1, 1);
> array_push ($arr1, $arr2); // <-- the important line


$arr1[] =& $arr2;

> array_push ($arr1, 2);
> array_push ($arr2, 3);
> array_push ($arr2, 4);
>
> // Contents of $arr2 are now empty
> // apparently $arr2 is inserted by value
> print_r ($arr1);
>
> echo "Expected output:\n";
> $arr1[1] = $arr2;
> print_r ($arr1);
> ?>
>
> What is the most obvious way to implement a tree-like structure in PHP? Is
> there a way to push arrays by reference rather than by value?
>
> BTW: I'm using PHP CLI version 5.2
>

Reply With Quote