Tree-like structure in PHP?

This is a discussion on Tree-like structure in PHP? within the PHP General forums, part of the PHP Programming Forums category; I'm trying to use arrays to implement a sort of data tree. For the code to work, it is ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-01-1970
Paul van Haren
 
Posts: n/a
Default Tree-like structure in PHP?

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
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
  #2 (permalink)  
Old 01-01-1970
Paul van Haren
 
Posts: n/a
Default Re: [PHP] Tree-like structure in PHP? (closed)

Thanks, the empasant helps!
Reply With Quote
  #3 (permalink)  
Old 11-16-2007
Robin Vickery
 
Posts: n/a
Default Re: [PHP] Tree-like structure in PHP?

On 16/11/2007, Paul van Haren <paul.vanharen@zonnet.nl> 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


array_push ($arr1, &$arr2);
or
$arr1[] =& $arr2;

-robin
Reply With Quote
  #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
  #5 (permalink)  
Old 11-16-2007
Richard Heyes
 
Posts: n/a
Default Re: [PHP] Tree-like structure in PHP?

> What is the most obvious way to implement a tree-like structure in PHP?

Use someone elses code that already works. :) Array based tree class is
here:

http://www.phpguru.org/Tree/Tree.phps

--
Richard Heyes
+44 (0)800 0213 172
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support
Reply With Quote
  #6 (permalink)  
Old 11-16-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Tree-like structure in PHP? (closed)

Paul van Haren wrote:
> Thanks, the empasant helps!


ampersand :-)

>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:25 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0