首頁 > 資料庫 > 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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板