PHP实现多级分类

Posted on 2021-12-27 20:58  君子不卿  阅读(255)  评论(0编辑  收藏  举报
function getTree($data) {
    $items = [];
    foreach ($data as $v){
        $items[$v['id']] = $v;
    }
    $tree = array(); //格式化好的树
    foreach ($items as $item)
        if (isset($items[$item['pid']]))
            $items[$item['pid']]['son'][] = &$items[$item['id']];
        else
            $tree[] = &$items[$item['id']];
    return $tree;
}

$sql = "select * from category where delete_time is null";
$rows = getAll($sql);
$list = getTree($rows);

Copyright © 2024 君子不卿
Powered by .NET 9.0 on Kubernetes

//评论