I have a recursive function that loops through an array. The function only returns $value (my result) sometimes, but the times that the function just returns NULL, i go in and print $value in the function and it's set to the correct value. i've printed out my key value/pairs in the recursive loop and the recursion is occuring properly as far as I can see. Here's my array:
Array
(
[index] => index
[company] => Array
(
[careers] => careers
[contact] => contact
)
[solutions] => Array
(
[information-technology] => Array
(
[business-processes] => business-processes
[network-strategy] => network-strategy
[virtual-private-networks-vpns] => virtual-private-networks-vpns
[network-security] => network-security
[disaster-recovery] => disaster-recovery
)
[site-development] => Array
(
[internet-strategies] => internet-strategies
[web-sites] => web-sites
[intranets] => intranets
[content-management] => content-management
)
)
[process] => process
[case-studies] => case-studies
[get-started] => get-started
)
Here's the function:
function grabChildren( $vals = array() )
{
if(count($vals) == 0)
{
$vals = $GLOBALS['menu']->tree;
}
foreach($vals as $key => $value)
{
if($key == $this->id)
{
return $value;
}
else
{
if(is_array($value) )
{
$this->grabChildren( $value );
}
}
}
}
And here's how I call it:
// parameter 1 here (solutions) is $this->id in my class, and it holds the proper value
$item = new menuItem('solutions', 'Solutions');
$children = $item->grabChildren();
print_r($children);
When I use solutions, company, process (the first key/vals in the array) as my first parameter, it returns fine. When I use anything in the second level (information-technology, site-development), it returns null. But i print $value in my function and the result is always correct (returns an array of children).
I also tried changing return $value to $this->children = $value; and when I print $item->children it always works. I need the function to return the value however, so this isnt' an option.
There are no empty values, the loops all work, it holds the variable to return, it just won't return. I've even tried
$return = $value; then doing
return $return; on the last line of the function and still nothing. But print_r($return) and i see the result.
Anyone have any ideas what I'm doing wrong here?