0

I am completely stumped on what appears to be a trivial problem.

I have this function:

function findCategoryNameInTree($id, $tree) {
    foreach($tree as $branch) {
        if ($branch['category_id'] == $id) {
             echo $branch['name'];//works
             print_r($branch['name']);//works
             //return($branch['name']); //returns nothing
             return $branch['name'];//fix this line per feedback still no return value
        } else {
            if(count($branch['children']) > 0) {
                findCategoryNameInTree($id,$branch['children']);
            }
        }

    }
}

I can't figure out for the life of me why it's not returning anything.

Please help!

Edit Here's how I call my function

//what I really want to do
$primgenre = findCategoryNameInTree($cat_id,$category_tree['children']);

//but this doesnt work either
echo $primgenre;

//nor this
print_r($pringenre);
1
  • 3
    You didn't show us the piece of the code that calls your findCategoryNameInTree function and how is it that you determine that function's return value is something that's wrong and not your interpretation of function's return value. Commented Dec 16, 2011 at 17:13

6 Answers 6

1

You forget to return for this :-

findCategoryNameInTree($id,$branch['children']);

Change :-

return findCategoryNameInTree($id,$branch['children']);
Sign up to request clarification or add additional context in comments.

3 Comments

Unbelievable, cant believe I missed that. Thank you so much! My code works with or without the parentheses everyone else suggested was the problem. -3 all for that.
if you want get -1, then ask more question like this. to get +1, your question has to be understandable
Didn't realize my question was unclear. Looking at it again I guess I didn't actually ASK anything. Thanks for the tip. Too much of a rush I guess.
1

here clearly mentioned

When returning an array, you should declare the array before the return, else the result is not as you expect;

Also see the NOTE

You should never use parentheses around your return variable when returning by reference, as this will not work. You can only return variables by reference, not the result of a statement.

foreach($tree as $branch) { }

here $branch is just an internal pointer of array ( assume as reference ) of $tree.

1 Comment

I'm not returning an array $branch['name'] is a string; Also, I removed the parentheses and I'm still getting an empty return value;
0

Just delete the round brackets, and then it should work!?

return $branch['name']; 

Thanks for the minus ;)

If your function works as to the point you say, calling

$test = findCategoryNameInTree(XY, "bla");
echo $test;

should output something.

3 Comments

Matters when you return by reference, so it's best to always leave it off.
I fixed this and it still returns no value; Any other ideas?
Didn't give you the minus btw
0

Try this:

function findCategoryNameInTree($id, $tree) {
foreach($tree as $branch) {
    if ($branch['category_id'] == $id) {
         x = $branch['name']; 
    } else {
        if(count($branch['children']) > 0) {
            findCategoryNameInTree($id,$branch['children']);
        }
    }

}
return x;

}

2 Comments

There's no reason you can't have the return within the foreach loop. In fact, it's better to have it in the loop because you don't have to wait for the loop to finish in order to return the desired value.
I tested the code and it works for me with all things i could imagine like you do the return, like I do with a break in the foreach and then return ...
0

This works for me, also the $my_result way!

<?php

$tree = ARRAY();
$tree[0] = ARRAY('category_id'=>10, 'name'=>'ten', 'children'=>'');
$tree[1] = ARRAY('category_id'=>11, 'name'=>'eleven', 'children'=>'');
$tree[2] = ARRAY('category_id'=>12, 'name'=>'twelve', 'children'=>'');


function findCategoryNameInTree($id, $tree) {
  //$my_result = 'none';
  foreach($tree as $branch) {
    if ($branch['category_id'] === $id) {
      //echo $branch['name'];//works
      //print_r($branch['name']);//works
      //$my_result = $branch['name'];
      //break; 
      return $branch['name']; //returns nothing
    }
    //else {
    // if(count($branch['children']) > 0) {
    //    findCategoryNameInTree($id,$branch['children']);
    //  }
    //}
  }
  //return $my_result;
}

echo findCategoryNameInTree(10, $tree).'<br />';
echo findCategoryNameInTree(11, $tree).'<br />';
echo findCategoryNameInTree(12, $tree).'<br />';

?>

Comments

0

Try this way:

$primgenre = findCategoryNameInTree($cat_id,$category_tree);

$branch is (should be) an array, so $tree is (should be). If $tree array contains well-formed $branch arrays everything should work fine. See my other solution with my example $tree array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.