<?php
namespace menutree;
class Menutree {
    
    public function menutree($menu){
        return $this->sort($menu);
    }

    public function sort($menu,$parentid=0,$level=0){

        static $arr=array();
        foreach ($menu as $k => $v) {
            if($v['parentid']==$parentid){
                $v['level']=$level;
                $arr[]=$v;
                $this->sort($menu,$v['id'],$level+1);
            }
        }

        return $arr;
    }

    //获取子栏目id

   public function childrenids($id,$obj){
        $data=$obj->field('id,parentid')->select();
        return $this->_childrenids($data,$id);
   }

   private function _childrenids($data,$id){
        static $arr=array();
        foreach ($data as $k => $v) {
            if($v['parentid']==$id){
                $arr[]=$v['id'];
                $this->_childrenids($data,$v['id']);
            }
        }

        return $arr;
   }

   //处理栏目排序
   public function cateSort($data,$obj){
    foreach ($data as $k => $v) {
        $obj->update(['id'=>$k,'listorder'=>$v]);
    }
   }

  

   
}