首页 > 数据库 > mysql教程 > 如何使用递归 PHP 函数检索树结构中特定父级的所有嵌套子节点?

如何使用递归 PHP 函数检索树结构中特定父级的所有嵌套子节点?

DDD
发布: 2024-11-06 11:48:02
原创
595 人浏览过

How to retrieve all nested child nodes of a specific parent in a tree structure using a recursive PHP function?

使用 PHP 设计递归函数检索嵌套子节点

在数据以邻接列表格式存储的情况下,递归函数可以有效检索子节点和孙节点特定父节点的节点。这种方法提供了一个定制的解决方案,用于识别给定父级下面的所有节点。

实现

以下是完成此任务的递归函数的实现:

function fetch_recursive($tree, $parent_id, $parent_found = false, $list = array())
{
    foreach ($tree as $key => $node) {
        if ($parent_found || $key == $parent_id) {
            $row_data = array();
            foreach ($node as $field => $value) {
                if ($field != 'children') {
                    $row_data[$field] = $value;
                }
            }
            $list[] = $row_data;

            if (isset($node['children']) && !empty($node['children'])) {
                $list = array_merge($list, fetch_recursive($node['children'], $parent_id, true));
            }
        } elseif (isset($node['children']) && !empty($node['children'])) {
            $list = array_merge($list, fetch_recursive($node['children'], $parent_id));
        }
    }

    return $list;
}
登录后复制

用法

假设您已经使用单独的函数从数据构建了树结构,您可以利用此用于检索特定父节点的所有子节点和孙节点的递归函数。

例如,如果您有一个结构如下的树:

Array
(
    [1] => Array
        (
            [id] => 1
            [name] => Electronics 
            [parent_id] => 0 
            [children] => Array
                (
                    [2] => Array
                        ( 
                            [id] => 2
                            [name] => Televisions 
                            [parent_id] => 1 
                            [children] => Array
                                (
                                    [4] => Array
                                        (
                                            [id] => 4
                                            [name] => Tube 
                                            [parent_id] => 2
                                            [children] => Array()
                                        )
                                    [5] => Array
                                        (
                                            [id] => 5
                                            [name] => LCD 
                                            [parent_id] => 2
                                            [children] => Array()
                                        )
                                    [6] => Array
                                        (
                                            [id] => 6
                                            [name] => Plasma 
                                            [parent_id] => 2
                                            [children] => Array()
                                        )
                                )
                        )
                    [3] => Array 
                        (
                            [id] => 3
                            [name] => Portable Electronics 
                            [parent_id] => 1
                            [children] => Array
                                (
                                    [7] => Array
                                        (
                                            [id] => 7
                                            [name] => Mp3 Players 
                                            [parent_id] => 3 
                                            [children] => Array
                                                (
                                                    [10] => Array
                                                        (
                                                            [id] => 10
                                                            [name] => Flash 
                                                            [parent_id] => 7
                                                            [children] => Array()
                                                        ) 
                                                )
                                        )
                                    [8] => Array 
                                        (
                                            [id] => 8
                                            [name] => CD Players 
                                            [parent_id] => 3
                                            [children] => Array()
                                        )
                                    [9] => Array 
                                        (
                                            [id] => 9
                                            [name] => 2 Way Radios 
                                            [parent_id] => 3
                                            [children] => Array()
                                        )
                                )
                        )
                )
        )
)
登录后复制

要检索其所有子节点,比如说, ID 为 3 的节点,您可以像这样调用该函数:

$parent_id = 3;
$child_nodes = fetch_recursive($tree, $parent_id);
登录后复制

$child_nodes 变量现在将包含一个包含所有子节点的数组ID为3的节点,如:

Array
(
    [0] => Array
        (
            [id] => 3
            [name] => Portable Electronics
            [parent_id] => 1
        )

    [1] => Array
        (
            [id] => 7
            [name] => Mp3 Players
            [parent_id] => 3
        )

    [2] => Array
        (
            [id] => 10
            [name] => Flash
            [parent_id] => 7
        )

    [3] => Array
        (
            [id] => 8
            [name] => CD Players
            [parent_id] => 3
        )

    [4] => Array
        (
            [id] => 9
            [name] => 2 Way Radios
            [parent_id] => 3
        )

)
登录后复制

以上是如何使用递归 PHP 函数检索树结构中特定父级的所有嵌套子节点?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板