thinkphp6树状结构子级

文章描述:

  common.php function get_tree($arr, $pid=0){ //$a […]

 

common.php

function get_tree($arr, $pid=0){
    //$arr为你查询出的数组
    // 检验必须为数组
    if (!is_array($arr)) {
        return false;
    }
    $tree = [];
    foreach ($arr as $k => $v) {
        if ($v['pid'] == $pid) {
            $v['children'] = get_tree($arr, $v['id']);
            // 最后一级下没有子级销毁child的
            if(empty($v['children'])){
                unset($v['children']);
            }
            $tree[] = $v;
        }
    }
    return $tree;
}

 

if ($type == 'tree') {
       $data = get_tree($data,0);
}

 

case:物料分类

发布时间:2025/02/17

发表评论