You are here: Re: Pimp my function « PHP Programming Language « IT news, forums, messages
Re: Pimp my function

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация