|
Posted by gosha bine on 06/26/07 08:40
On 26.06.2007 08:40 Lars Eighner wrote:
> My function GetTourDescendants works, but I do not really have a recursive
> brain (and run from lisp like a vampire from a crucifix) and am a little
> wary of recursion in PHP. And besides, the function is ugly as sin. So,
> this question is not will this work (it does) or how to make it work (it
> does), but whether there is a better way or not to do this.
> [snip]
>
hi there
a general purpose tree-walker algorithm is as simple as
$node = array(
'data' => whatever data,
'children' => array of nodes or empty
);
function walk($node) {
// do something with $node['data']
if(isset($node['children']))
foreach($node['children'] as $child)
walk($child);
}
a more generic approach would be to pass a callback function to the
walker, so that you can use it for different things
function walk($node, $callback) {
call_user_func($callback, $node);
if(isset($node['children']))
foreach($node['children'] as $child)
walk($child);
}
--
gosha bine
extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Navigation:
[Reply to this message]
|