PHP处理无限层级分类的递归函数

1

php 复制代码
function getCategoryTree($categories, $parent_id = 0) {
    $tree = array();
    
    foreach ($categories as $category) {
        if ($category['parent_id'] == $parent_id) {
            $children = getCategoryTree($categories, $category['id']);
            if ($children) {
                $category['children'] = $children;
            }
            $tree[] = $category;
        }
    }
    
    return $tree;
}

// 示例数据
$categories = array(
    array('id' => 1, 'name' => 'Category 1', 'parent_id' => 0),
    array('id' => 2, 'name' => 'Category 2', 'parent_id' => 0),
    array('id' => 3, 'name' => 'Category 1.1', 'parent_id' => 1),
    array('id' => 4, 'name' => 'Category 1.2', 'parent_id' => 1),
    array('id' => 5, 'name' => 'Category 1.1.1', 'parent_id' => 3),
);

$tree = getCategoryTree($categories);
print_r($tree);

在上面的例子中,getCategoryTree函数接受一个包含所有分类的数组和一个父分类ID作为参数,并递归地构建一个树形结构的数组。通过调用getCategoryTree函数并传入示例数据后,将会输出一个包含层级关系的分类树。您可以根据实际需求修改示例数据和函数逻辑。

2

php 复制代码
// 模拟分类数据
$categories = array(
    array('id' => 1, 'name' => '电子产品', 'parent_id' => 0),
    array('id' => 2, 'name' => '手机', 'parent_id' => 1),
    array('id' => 3, 'name' => '笔记本电脑', 'parent_id' => 1),
    array('id' => 4, 'name' => '家用电器', 'parent_id' => 0),
    array('id' => 5, 'name' => '电视', 'parent_id' => 4),
    array('id' => 6, 'name' => '冰箱', 'parent_id' => 4),
    array('id' => 7, 'name' => '空调', 'parent_id' => 4),
    array('id' => 8, 'name' => '服饰', 'parent_id' => 0),
    array('id' => 9, 'name' => '男装', 'parent_id' => 8),
    array('id' => 10, 'name' => '女装', 'parent_id' => 8),
    array('id' => 11, 'name' => '鞋类', 'parent_id' => 0),
    array('id' => 12, 'name' => '男鞋', 'parent_id' => 11),
    array('id' => 13, 'name' => '女鞋', 'parent_id' => 11),
    array('id' => 14, 'name' => '数码产品', 'parent_id' => 1),
    array('id' => 15, 'name' => '相机', 'parent_id' => 14),
    array('id' => 16, 'name' => '摄像机', 'parent_id' => 14),
    array('id' => 17, 'name' => '办公用品', 'parent_id' => 0),
    array('id' => 18, 'name' => '文具', 'parent_id' => 17),
    array('id' => 19, 'name' => '办公设备', 'parent_id' => 17),
    array('id' => 20, 'name' => '电脑配件', 'parent_id' => 1),
    array('id' => 21, 'name' => '显示器', 'parent_id' => 20),
    array('id' => 22, 'name' => '内存条', 'parent_id' => 20),
    array('id' => 23, 'name' => '游戏机', 'parent_id' => 1),
    array('id' => 24, 'name' => 'PS5', 'parent_id' => 23),
    array('id' => 25, 'name' => 'Switch', 'parent_id' => 23),
);
// 递归处理分类数据
function buildTree($categories, $parent_id = 0) {
    $tree = array();
    foreach ($categories as $category) {
        if ($category['parent_id'] == $parent_id) {
            $children = buildTree($categories, $category['id']);
            if ($children) {
                $category['children'] = $children;
            }
            $tree[] = $category;
        }
    }
    return $tree;
}
// 构建树形结构
$tree = buildTree($categories);
// 显示分类数据
function showTree($tree, $indent = 0) {
    foreach ($tree as $category) {
        echo str_repeat("--", $indent) . $category['name'] . "<br>";
        if (isset($category['children'])) {
            showTree($category['children'], $indent + 1);
        }
    }
}
// 调用函数显示树形结构
showTree($tree);</pre></div>
相关推荐
01_ice17 小时前
html个人简历展示
前端·html
程序员爱钓鱼17 小时前
Rust where详解:让泛型与Trait约束更加清晰
前端·后端·rust
青 春 记 忆17 小时前
LeetCode 155. 最小栈|Python 解法详解
开发语言·python·leetcode
zzzzzz31018 小时前
react-bits 为什么会吸引前端开发者:别把动效当装饰,先把它当成页面能力
前端·react.js·动效
风月说与山鬼20 小时前
二、React入口文件(main.jsx)
前端·react.js
东风破_1 天前
Docker 基础:为什么需要容器?
前端·后端·nginx
东风破_1 天前
Docker 实战:使用 Nginx 作为容器入口代理 Node 服务
前端·后端·nginx
你别说话了1 天前
Vue项目解决跨域
前端·javascript·vue.js
唐青枫1 天前
http-server:别再双击 index.html,一条命令把目录变成网站
前端·javascript
程序员三藏1 天前
Python+requests实现接口自动化测试
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试