美文网首页
php、golang无限分类

php、golang无限分类

作者: 王宣成 | 来源:发表于2021-11-12 10:45 被阅读0次
    <?php 
    $categories = array(
        array('id'=>1,'name'=>'电脑','pid'=>0),
        array('id'=>2,'name'=>'手机','pid'=>0),
        array('id'=>3,'name'=>'笔记本','pid'=>1),
        array('id'=>4,'name'=>'台式机','pid'=>1),
        array('id'=>5,'name'=>'智能机','pid'=>2), 
        array('id'=>6,'name'=>'功能机','pid'=>2), 
        array('id'=>7,'name'=>'超级本','pid'=>3),
        array('id'=>8,'name'=>'游戏本','pid'=>3),
    );
    
    echo "<pre>";
    $tree = getDataTree($categories);
    print_r($tree);
    
    function getDataTree($rows, $id='id',$pid = 'pid',$child = 'child',$root=0) {
        $tree = array();
        if(is_array($rows)){
            $array = array();
            foreach ($rows as $key=>$item){
                $rows[$key]['level'] = 0;
                $rows[$key]['str'] = '';
                $array[$item[$id]] = &$rows[$key];
            }
    
            foreach($rows as $key=>$item){
                $parentId = $item[$pid];
                if($root == $parentId){
                    $tree[] = &$rows[$key];
                }else{
                    if(isset($array[$parentId])){
                        $parent = &$array[$parentId];
                        $rows[$key]['level'] = $array[$parentId]['level']+1;
                        $rows[$key]['str'] = str_repeat("--",$rows[$key]['level']);
                        $parent[$child][] = &$rows[$key];
                    }
                }
            }
    
        }
    
        return $tree;
    }
    
    
    // 递归
    function getTree($data, $parent_id = 0,$level=0)
    {
        $tree = array();
        foreach ($data as $k => $v) {
            if ($v["pid"] == $parent_id) {
                unset($data[$k]);
                $v["level"] = $level;
                if (!empty($data)) {
                    $children = getTree($data, $v["id"],$v['level']+1);
                    if (!empty($children)) {
                        $v["child"] = $children;
                    }
                }
                $tree[] = $v;
            }
        }
        return $tree;
    }
    
    
    
    // model/articleCategory.go
    package model
    
    type TreeList struct {
        ID       int
        ParentID int
        Name     string
        //Level    int
        Children []TreeList
    }
    
    
    func GetMenu(menuList []model.TreeList,pid int) []model.TreeList {
        var treeList  []model.TreeList
        for _, v := range menuList {
            if v.ParentID == pid {
                child := GetMenu(menuList,v.ID)
                node := model.TreeList {
                    ID: v.ID,
                    Name: v.Name,
                    ParentID: v.ParentID,
                    Children:child,
                }
    
                //strings.Repeat(s,2)
                treeList = append(treeList, node)
            }
        }
        return treeList
    }
    

    相关文章

      网友评论

          本文标题:php、golang无限分类

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