php无限极分类

   public function menu()
    {
        $model = new Menu();
        $data = $model->menuAll();
        $list = $this->demo($data,0);
        return json_encode(["code"=>101,"message"=>'返回菜单成功',"menudata"=>$list]);
    }




public function get_tree($arr,$pk='sid',$upid='pid',$child='child')
{
    $items = array();
             foreach($arr as $val){
                 $items[$val['sid']] = $val;
     }
    $tree = array();
     foreach($items as $k=>$val){
                if(isset($items[$val[$upid]])){
                         $items[$val[$upid]][$child][]=&$items[$k];
         }else{
                         $tree[] = &$items[$k];
         }
    }
    return $tree;





}

 

/*
 * 递归生成菜单
 *
 * */
public function demo($data,$pid)
{
    $tree = '';
    foreach($data as $k => $v)
    {
        if($v['pid'] == $pid)
        {         //父亲找到儿子
            $v['children'] =$this->demo($data, $v['mid']);
            $tree[] = $v;
            unset($data[$k]);
        }
    }
    return $tree;

}



 /**
     * 递归获取所有的子分类的ID
     * */
    public function get_all_child($array,$id){
        $arr = array();
        foreach($array as $v){
            if($v['pc_pid'] == $id){
                $arr[] = $v['pc_sid'];
                $arr = array_merge($arr,$this->get_all_child($array,$v['pc_sid']));
            };
        };
        return $arr;
    }

 

 

 

你可能感兴趣的:(php,js)