/**
*向下递归查询包含的子集数组
*@param $parent_id 父id数组
*@param
*/
public function get_childs( $parent_id = array(), $level = 0 ){
$id_arr = array();
$id_arr= $thisModel->where('pid','in',$parent_id)->column('id');
//限制3级
if (!empty($id_arr)&& $level<3) {
$level++;
$id_arr=array_merge($id_arr,get_childs($id_arr,$level));
}
return $id_arr;
}
/**
*向上递归查询数组
*@param $pid 父id
*@param $array 父类数组
*/
public function getParent( $pid ,$array=[]) {
$level = 1;
$is_parent =$thisModel->where(["id"=>$pid])->find();
$array[] = $is_parent;
if ( $is_parent["pid"] ) {
$level++;
return $this->getParent( $is_parent['pid'],$array);
}
return $array;
}
//调用
$userInfo = $cateModel->where('id',10)->find();
//向上找父类
if ($userInfo['pid']>0) {
$this->getParent($userInfo['pid']);
}
//向下找子集
$this->get_childs([10,11]);
递归向上父类 向下查询子集
最新推荐文章于 2023-11-29 17:10:25 发布