【发布时间】:2020-08-27 10:04:17
【问题描述】:
我正在寻找一个可以创建分层数组但仅适用于某些孩子的函数。
这个例子似乎不错: Recursive function to generate multidimensional array from database result
但我想要父母只是为了一些 ID。例如:
+-------+---------------+---------------------------+
| id | parent_id | title |
+-------+---------------+---------------------------+
| 1 | 0 | Parent Page |
| 2 | 1 | Sub Page |
| 3 | 2 | Sub Sub Page |
| 4 | 0 | Another Parent Page |
| 5 | 1 | Sub Page 2 |
+-------+---------------+---------------------------+
我只想要 id 2、4 和 5 的层次结构。
然后给我这样的东西:
Array
(
[0] => Array
(
[id] => 1
[parent_id] => 0
[title] => Parent Page
[children] => Array
(
[0] => Array
(
[id] => 2
[parent_id] => 1
[title] => Sub Page
),
[1] => Array
(
[id] => 5
[parent_id] => 1
[title] => Sub Page 2
)
)
)
[1] => Array
(
[id] => 4
[parent_id] => 0
[title] => Another Parent Page
)
)
2、4 和 5 应该是最小的孩子,它们下面没有任何东西。
继续,我想要的与链接的帖子完全相同,但我最小的叶子应该只是数组 [2,4,5] 中存在的叶子 id
不知道有没有人理解我的问题...
非常感谢
编辑:我更新并添加了 id = 5 的示例。
【问题讨论】: