Re: Creating Tree Structure from associative array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





Robert Cummings wrote:
> 
> On Thu, 2006-10-19 at 23:58 -0500, Larry Garfield wrote:
>> That depends on what your data structure is, exactly, and what sort of
>> tree 
>> structure you want on the other side.  Please be more specific.
>> 
>> On Thursday 19 October 2006 09:08, Angelo Zanetti wrote:
>> > Hi all,
>> >
>> > I have an associative array, which contains parent and child
>> > relationships. I've searched the web for creating a tree structure from
>> > this and found a few good sites but doesnt help 100% perhaps someone
>> can
>> > point me in the correct direction? I've started to code it got to a
>> > point where I cant go any further, the code is pseudo code and dont
>> want
>> > to reinvent the wheel.
>> >
>> > any suggestions would be really appreciated.
> 
> It's kinda simple...
> 
> <?php
> 
> ////////////////////////////////
> //
> //        6           5
> //      /   \       /   \
> //     2     7     9     3
> //   / | \
> //  1  4  8
> //
> ////////////////////////////////
> 
> $list = array
> (
>     array
>     (
>         'id'    => '1',
>         'pid'   => '2',
>         'value' => 'Value Foo 1',
>     ),
>     array
>     (
>         'id'    => '2',
>         'pid'   => '6',
>         'value' => 'Value Foo 2',
>     ),
>     array
>     (
>         'id'    => '3',
>         'pid'   => '5',
>         'value' => 'Value Foo 3',
>     ),
>     array
>     (
>         'id'    => '4',
>         'pid'   => '2',
>         'value' => 'Value Foo 4',
>     ),
>     array
>     (
>         'id'    => '5',
>         'pid'   => '0',
>         'value' => 'Value Foo 5',
>     ),
>     array
>     (
>         'id'    => '6',
>         'pid'   => '0',
>         'value' => 'Value Foo 6',
>     ),
>     array
>     (
>         'id'    => '7',
>         'pid'   => '6',
>         'value' => 'Value Foo 7',
>     ),
>     array
>     (
>         'id'    => '8',
>         'pid'   => '2',
>         'value' => 'Value Foo 8',
>     ),
>     array
>     (
>         'id'    => '9',
>         'pid'   => '5',
>         'value' => 'Value Foo 9',
>     ),
> );
> 
> //
> // Set up indexing of the above list (in case it wasn't indexed).
> //
> $lookup = array();
> foreach( $list as $item )
> {
>     $item['children'] = array();
>     $lookup[$item['id']] = $item;
> }
> 
> //
> // Now build tree.
> //
> $tree = array();
> foreach( $lookup as $id => $foo )
> {
>     $item = &$lookup[$id];
>     if( $item['pid'] == 0 )
>     {
>         $tree[$id] = &$item;
>     }
>     else
>     if( isset( $lookup[$item['pid']] ) )
>     {
>         $lookup[$item['pid']]['children'][$id] = &$item;
>     }
>     else
>     {
>         $tree['_orphans_'][$id] = &$item;
>     }
> }
> 
> //
> // WooooooohoooooooooOO!
> //
> print_r( $tree );
> 
> ?>
> 
> Cheers,
> Rob.
> -- 
> .------------------------------------------------------------.
> | InterJinn Application Framework - http://www.interjinn.com |
> :------------------------------------------------------------:
> | An application and templating framework for PHP. Boasting  |
> | a powerful, scalable system for accessing system services  |
> | such as forms, properties, sessions, and caches. InterJinn |
> | also provides an extremely flexible architecture for       |
> | creating re-usable components quickly and easily.          |
> `------------------------------------------------------------'
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
> 
for web browser:
// WooooooohoooooooooOO!
//
echo '<pre>';
print_r( $tree );

-- 
View this message in context: http://www.nabble.com/Creating-Tree-Structure-from-associative-array-tf2473585.html#a6920126
Sent from the PHP - General mailing list archive at Nabble.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux