Re: [PHP] Creating Tree Structure from associative array
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; } } // // WooohoOO! // 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: // WooohoOO! // 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
Re: [PHP] Creating Tree Structure from associative array
On Fri, 2006-10-20 at 09:20 -0700, Jürgen Wind wrote: for web browser: // WooohoOO! // echo 'pre'; print_r( $tree ); True, but I do quick sample scripts from the command-line :D 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
Re: [PHP] Creating Tree Structure from associative array
Robert Cummings wrote: On Fri, 2006-10-20 at 09:20 -0700, Jürgen Wind wrote: for web browser: // WooohoOO! myself, I'm more partial to // YeeHaw! // echo 'pre'; print_r( $tree ); True, but I do quick sample scripts from the command-line :D Cheers, Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Creating Tree Structure from associative array
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. Thanks in advance -- Angelo Zanetti Systems developer *Telephone:* +27 (021) 469 1052 *Mobile:* +27 (0) 72 441 3355 *Fax:*+27 (0) 86 681 5885 * Web:* http://www.zlogic.co.za *E-Mail:* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Creating Tree Structure from associative array
# [EMAIL PROTECTED] / 2006-10-19 16:08:58 +0200: 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. I don't think there's any graph-manipulation library for PHP (there's a bit of addEdge(), addNode() stuff in Image_GraphViz[1] but it doesn't seem useful). Until someone wraps Agraph or Boost.Graph in a module: check out Iterator, Builder, and Composite in Design Patterns[2]. [1] http://pear.php.net/package/Image_GraphViz [2] http://www.amazon.com/dp/0201633612 -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Creating Tree Structure from associative array
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. Thanks in advance -- Angelo Zanetti Systems developer *Telephone:* +27 (021) 469 1052 *Mobile:* +27 (0) 72 441 3355 *Fax:*+27 (0) 86 681 5885 * Web:* http://www.zlogic.co.za *E-Mail:* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] -- Larry Garfield AIM: LOLG42 [EMAIL PROTECTED] ICQ: 6817012 If nature has made any one thing less susceptible than all others of exclusive property, it is the action of the thinking power called an idea, which an individual may exclusively possess as long as he keeps it to himself; but the moment it is divulged, it forces itself into the possession of every one, and the receiver cannot dispossess himself of it. -- Thomas Jefferson -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Creating Tree Structure from associative array
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; } } // // WooohoOO! // 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