美文网首页
菜单的无限极分类实现

菜单的无限极分类实现

作者: 3c9a691b4944 | 来源:发表于2017-12-25 13:19 被阅读30次

    原文链接:https://www.bestqliang.com/#/article/4

    /**
     * 递归
     */
    public function getTree($data, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0)
    {
        $tree = array();
        foreach($data as $k => $v) {
            if($v[$pid] == $root) {
                //父亲找到儿子
                $v[$child] = $this->getTree($data, $pk, $pid, $child, $v[$pk]);
                $tree[] = $v;
            }
        }
        return $tree;
    }
    
    /** 
     * 迭代
     */
    public function getTree($list, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0)
    {
        $tree     = array();
        $packData = array();
        foreach ($list as $data) {
            $packData[$data[$pk]] = $data;
        }
        foreach ($packData as $key => $val) {
            if ($val[$pid] == $root) {
                //代表跟节点
                $tree[] = &$packData[$key];
            } else {
                //找到其父类
                $packData[$val[$pid]][$child][] = &$packData[$key];
            }
        }
        return $tree;
    }
    

    相关文章

      网友评论

          本文标题:菜单的无限极分类实现

          本文链接:https://www.haomeiwen.com/subject/kzrlgxtx.html