php无限级分类递归+非递归

递归

function buildTree($data, $parentId = 0) {
    $tree = array();
    foreach ($data as $item) {
        if ($item['parent_id'] == $parentId) {
            $children = buildTree($data, $item['id']);
            if ($children) {
                $item['children'] = $children;
            }
            $tree[] = $item;
        }
    }
    return $tree;
}

// 示例数据
$categories = array(
    array('id' => 1, 'name' => '分类1', 'parent_id' => 0),
    array('id' => 2, 'name' => '分类1-1', 'parent_id' => 1),
    array('id' => 3, 'name' => '分类1-2', 'parent_id' => 1),
    array('id' => 4, 'name' => '分类1-1-1', 'parent_id' => 2),
    array('id' => 5, 'name' => '分类2', 'parent_id' => 0),
    array('id' => 6, 'name' => '分类2-1', 'parent_id' => 5),
);

$tree = buildTree($categories);
echo json_encode($tree);

非递归

function buildTreeIterative($data) {
    $tree = array();
    $flat = array();
    
    foreach ($data as $item) {
        $flat[$item['id']] = $item;
    }
    
    foreach ($flat as $item) {
        if ($item['parent_id'] == 0) {
            $tree[] = &$flat[$item['id']];
        } else {
            $flat[$item['parent_id']]['children'][] = &$flat[$item['id']];
        }
    }
    
    return $tree;
}

// 示例数据
$categories = array(
    array('id' => 1, 'name' => '分类1', 'parent_id' => 0),
    array('id' => 2, 'name' => '分类1-1', 'parent_id' => 1),
    array('id' => 3, 'name' => '分类1-2', 'parent_id' => 1),
    array('id' => 4, 'name' => '分类1-1-1', 'parent_id' => 2),
    array('id' => 5, 'name' => '分类2', 'parent_id' => 0),
    array('id' => 6, 'name' => '分类2-1', 'parent_id' => 5),
);

$tree = buildTreeIterative($categories);
echo json_encode($tree);

通过 pid 获取所有上级分类 常用于面包屑导航

function getParentsByParentId($data = [], $parent_id)
{
    static $categories = [];

    if ($data && is_array($data)) {
        foreach ($data as $item) {
            if ($item['id'] == $parent_id) {
                $categories[] = $item;
                getParentsByParentId($data, $item['parent_id']);
            }
        }
    }
    return $categories;
}

function getParentsByParentId2($data = [], $parent_id)
{
    $categories = [];

    if ($data && is_array($data)) {
        foreach ($data as $item) {
            if ($item['id'] == $parent_id) {
                $categories[] = $item;
                $categories = array_merge($categories, getParentsByParentId2($data, $item['parent_id']));
            }
        }
    }
    return $categories;
}

带level的递归

function make_tree2($data = [], $parent_id = 0, $level = 0)
{
    $tree = [];
    if ($data && is_array($data)) {
        foreach ($data as $v) {
            if ($v['parent_id'] == $parent_id) {
                $tree[] = [
                    'id' => $v['id'],
                    'level' => $level,
                    'name' => $v['name'],
                    'parent_id' => $v['parent_id'],
                    'children' => make_tree2($data, $v['id'], $level + 1),
                ];
            }
        }
    }
    return $tree;
}
posted @   朝阳1  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示