这对我有用,也是$ my_result方式!
$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).'
';
echo findCategoryNameInTree(11, $tree).'
';
echo findCategoryNameInTree(12, $tree).'
';
?>