無限級遞歸

+++++++++++++++++++++++++++++++無限級遞歸+++++++++++++++++++++++++++
例一:
public function recursion(){git

  $ar= $this->Cate($gittable,0,0);數組

  return view('home.home3',['arr1' => $ast,'arr2' => $navigation,'arr3' => $data]);
}緩存


public function Cate(&$info, $child, $pid)
{
  $child = array();
  if(!empty($info)){//當$info中的子類尚未被移光的時候
    foreach ($info as $k => &$v) {
      if($v->Pid == $pid){//判斷是否存在子類pid和返回的父類id相等的
        $v->child = $this->Cate($info, $child, $v->Tid);//每次遞歸參數爲當前的父類的id
        $child[] = $v;//將$info中的鍵值移動到$child當中
        unset($info[$k]);//每次移動過去以後刪除$info中當前的值
       }
     }
   }
   return $child;//返回生成的樹形數組
} this


例二:
public function recursion(){spa

  $model = new Recursion();//調用model層

  $navigation = $model->navigation();//調用地區遞歸

  $gittable = $model->gittable();//調用查詢資源

  $ast = $this->Cate($gittable,0,0);it

  return view('home.home3',['arr1' => $ast,'arr2' => $navigation,'arr3' => $data]);//展現給用戶
}io

public function Cate(&$info, $child, $pid)
{
  $child = array();
  if(!empty($info)){
  foreach ($info as $k => &$v) {
    if($v->p_id == $pid){
      $v->child = $this->Cate($info, $child, $v->r_id);
      $child[] = $v;
      unset($info[$k]);
    }
  }
  }
  return $child;//返回生成的樹形數組
}table


++++++++++++++++++++++++++++++++逆歸+++++++++++++++++++++++++++++++
倒着來

 

以上兩種方式太浪費資源,建議將遞歸的值,一組一組的存入緩存中,將節省大量資源!!!

相關文章
相關標籤/搜索