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

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

DDD
DDD原创
2024-11-06 11:48:02505浏览

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn