True way or not true way?

Feed 4 posts, 1 voices

Avatar
3 posts

In one of my project i was need collect all elements of site tree without childs (“leafs”) for further operations (for example random output in main page). I’m modify function from Frog CMS documentation (snippet Sitemap):

function get_elements_uri($parent, $out=array())
{
    $childs = $parent->children();
    if (count($childs) > 0)
    {
        foreach ($childs as $child)
	{
		if (count($child->children()) == 0)
		{
			$out[] = $child->url;
			get_elements_uri($child, $out);
		}
		else
			$out = get_elements_uri($child, $out);
        }
    }
    return $out;
}

And use it like this:

$elements = get_elements_uri($this->find('/'));

foreach ($elements as $element) {
    $current_element = $this->find($element);
    // ... do something with $current_element ...
}

I have one question. It’s “true way”? :) Or having some another methods of solution this task? My doubts consist in second call of find() function, which placed in foreach.

Thank you! And sorry for bad English :)