遞歸參考

function get_cat($cid=0){//遞歸算法    
        $nav=M('nav');
        $cat=$nav->where('pre_id='.$cid)->select();
        foreach($cat as $key=>$val){
            $cats=$this->get_cat($val['id']);
            $cat[$key]['child']=$cats;
        }
        return $cat;
    }算法

 

Array
(
 [0] => Array  (  [id] => 4  [nav_name] => 研報  [pre_id] => 0  [pid] => 4  [child] => Array  (  [0] => Array  (  [id] => 5  [nav_name] => 宏觀  [pre_id] => 4  [pid] => 17  [child] => Array  (  )   )   [1] => Array  (  [id] => 6  [nav_name] => 行業  [pre_id] => 4  [pid] => 18  [child] => Array  (  )   )   [2] => Array  (  [id] => 7  [nav_name] => 個股  [pre_id] => 4  [pid] => 19  [child] => Array  (  )   )   [3] => Array  (  [id] => 8  [nav_name] => 名家  [pre_id] => 4  [pid] => 20  [child] => Array  (  )   )   )   )   [1] => Array  (  [id] => 2  [nav_name] => 市場  [pre_id] => 0  [pid] => 2  [child] => Array  (  [0] => Array  (  [id] => 9  [nav_name] => 大盤  [pre_id] => 2  [pid] => 9  [child] => Array  (  )   )   [1] => Array  (  [id] => 10  [nav_name] => 主力  [pre_id] => 2  [pid] => 10  [child] => Array  (  )   )   [2] => Array  (  [id] => 11  [nav_name] => 基金  [pre_id] => 2  [pid] => 11  [child] => Array  (  )   )   [3] => Array  (  [id] => 12  [nav_name] => 策略  [pre_id] => 2  [pid] => 12  [child] => Array  (  )   )   )   )   [2] => Array  (  [id] => 3  [nav_name] => 理財  [pre_id] => 0  [pid] => 3  [child] => Array  (  [0] => Array  (  [id] => 13  [nav_name] => 銀行  [pre_id] => 3  [pid] => 13  [child] => Array  (  )   )   [1] => Array  (  [id] => 14  [nav_name] => 債券  [pre_id] => 3  [pid] => 14  [child] => Array  (  )   )   [2] => Array  (  [id] => 15  [nav_name] => 保險  [pre_id] => 3  [pid] => 15  [child] => Array  (  )   )   [3] => Array  (  [id] => 16  [nav_name] => 信託  [pre_id] => 3  [pid] => 16  [child] => Array  (  )   )   )   )   [3] => Array  (  [id] => 1  [nav_name] => 全球  [pre_id] => 0  [pid] => 1  [child] => Array  (  [0] => Array  (  [id] => 17  [nav_name] => 美股  [pre_id] => 1  [pid] => 5  [child] => Array  (  )  ) [1] => Array ( [id] => 18 [nav_name] => 外匯 [pre_id] => 1 [pid] => 6 [child] => Array ( ) ) [2] => Array ( [id] => 19 [nav_name] => 期貨 [pre_id] => 1 [pid] => 7 [child] => Array ( ) ) [3] => Array ( [id] => 20 [nav_name] => 黃金 [pre_id] => 1 [pid] => 8 [child] => Array ( ) ) ) ) ) 
相關文章
相關標籤/搜索