递归函数返回null值

时间:2014-02-03 05:48:09

标签: php recursion

enter image description here

我使用递归函数从上面的表结构中获取类似访问控制/ CARDS / FOBS 的类别完整路径,但我的递归函数返回空值。

function xyz($id,$parent) {

if($parent == '0') {
   //my code working fine
   //return 
} 
else 
{
    $catid = $id; //here 25 coming

    $cat_array        =  array();
    $category_array   =  $this->Recursive($catid,$cat_array);   
    //echo $category_array;exit; getting Null result
    return $category_array ;

    }

}

function Recursive($catid,$cat_array) {

  $sql       = mysql_query("Select bg_category_id,parent_id,title from categories_list 
               Where bg_category_id = '".$catid."'");
  $result    = mysql_fetch_array($sql);

  array_push($cat_array,$result['title']); 

  if($result['parent_id'] != '0') {

     $this->Recursive($result['parent_id'],$cat_array) ;

  } else {  

    if(count($cat_array) > 0){
       $k =  implode("/",$cat_array);
     }
     //echo $k;exit; getting desired result FOBS/CARDS/Access Control 
    return $k;

  }

if($parent == '0') { //my code working fine //return } else { $catid = $id; //here 25 coming $cat_array = array(); $category_array = $this->Recursive($catid,$cat_array); //echo $category_array;exit; getting Null result return $category_array ; }

1 个答案:

答案 0 :(得分:3)

你需要在递归时返回递归函数,否则它将不返回任何内容。

if($result['parent_id'] != '0') {

   return $this->Recursive($result['parent_id'],$category_array) ;

 }