Recursing through an array
| From: | Simon H | Date: | Mon, 21 Jul 2003 22:18:16 +0000 |
| Subject: | Recursing through an array | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-9936@lists.php.net to get a copy of this message | ||
Hello there
I've been working with Pear::Tree to generate a tree array which I would
then like to recurse through and indent for every child of a parent
throughout the depth of the tree.
The example array (shortened) to build the tree is:
Array (
[0] => Array (
[0] => Array (
[id] => 2
[parentId] => 1
)
[1] => Array (
[id] => 5
[parentId] => 2
)
[2] => Array (
[id] => 3
[parentId] => 2
)
[3] => Array (
[id] => 4
[parentId] => 3
)
[4] => Array (
[id] => 9
[parentId] => 4
)
[5] => Array (
[id] => 6
[parentId] => 3
)
[6] => Array (
[id] => 7
[parentId] => 3
)
[7] => Array (
[id] => 8
[parentId] => 7
)
)
)
In my template I have no idea (have even tried compiler functions and
templates suggested on the forum) how to recurse through this array
using the parent ID to get something like this (dynamically created of
course):
<ul>
<li>
[id] => 2
[parentId] => 1
<ul>
<li>
[id] => 5
[parentId] => 2
</li>
<li>
[id] => 3
[parentId] => 2
</li>
<li>
<ul>
<li>
[id] => 4
[parentId] => 3
</li>
<li>
<ul>
<li>
[id] => 9
[parentId] => 4
</li>
</ul>
</li>
<li>
[id] => 6
[parentId] => 3
</li>
<li>
[id] => 7
[parentId] => 3
</li>
<li>
<ul>
<li>
[id] => 8
[parentId] => 4
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
What I'm after is something that can be totally dynamic in terms of
depth...Any ideas anyone?
This is doin' my head in!
Thanks in advance!
--
Simon H