提问人:SULTAN 提问时间:10/10/2023 更新时间:10/10/2023 访问量:43
使用函数 (php) 递归获取第 n 个子 ID
Getting nth-children ids recursively using function (php)
问:
我有一个包含两列的表格和id
parent_id
|-id-|-parent_id-|
| 1 | 0 |
| 2 | 1 |
| 3 | 16 |
| 4 | 19 |
| 5 | 19 |
| 6 | 2 |
| 7 | 2 |
| 8 | 6 |
| 9 | 19 |
| 10 | 4 |
| 11 | 4 |
| 12 | 2 |
| 13 | 19 |
| 14 | 13 |
| 15 | 2 |
| 16 | 19 |
| 17 | 13 |
| 18 | 19 |
| 19 | 1 |
|-id-|-parent_id-|
我正在尝试递归地获取所有孩子
因此,我制作了一个函数来检查每个函数,如果这有子项,我会将它们推送到数组中,但由于某种原因,这不起作用!getRecursiveChildren()
id
id
这是我的代码:
<?php
$passed_parent_id=1;
$q = mysqli_query($link, "SELECT * FROM `_del_test` WHERE `isActive` = '1' AND `parent_id` = '$passed_parent_id'");
if($q&&mysqli_num_rows($q)>0)
{
# get all kids
while($qr=mysqli_fetch_assoc($q))
{
$sys_id = $qr['sys_id'];
$parent_id = $qr['parent_id'];
getRecursiveChildren($sys_id,$parent_id);
}
}
function getRecursiveChildren($id, $items): array
{
$kids = [];
foreach ($items as $key => $item) {
if ($item['parent_id'] === $id) {
$kids[] = $key;
if ($id !== $key) {
array_push($kids, ...getRecursiveChildren($key, $items));
}
}
}
return $kids;
}
?>
你能帮忙吗?!我的大脑被卡住了,我再也无法思考了 谢谢
答:
0赞
lukas.j
10/10/2023
#1
这将按照项目>子项>子项>项>项>子项>子项>项的顺序创建一个数组:
$input = [
[ 'id' => 1, 'parent_id' => 0 ],
[ 'id' => 2, 'parent_id' => 1 ],
[ 'id' => 3, 'parent_id' => 16 ],
[ 'id' => 4, 'parent_id' => 19 ],
[ 'id' => 5, 'parent_id' => 19 ],
[ 'id' => 6, 'parent_id' => 2 ],
[ 'id' => 7, 'parent_id' => 2 ],
[ 'id' => 8, 'parent_id' => 6 ],
[ 'id' => 9, 'parent_id' => 19 ],
[ 'id' => 10, 'parent_id' => 4 ],
[ 'id' => 11, 'parent_id' => 4 ],
[ 'id' => 12, 'parent_id' => 2 ],
[ 'id' => 13, 'parent_id' => 19 ],
[ 'id' => 14, 'parent_id' => 13 ],
[ 'id' => 15, 'parent_id' => 2 ],
[ 'id' => 16, 'parent_id' => 19 ],
[ 'id' => 17, 'parent_id' => 13 ],
[ 'id' => 18, 'parent_id' => 19 ],
[ 'id' => 19, 'parent_id' => 1 ]
];
function extractChildren(array $array, int $parent_id): array {
return
array_merge(
...array_map(
fn($item) => [ $item['id'], ...extractChildren($array, $item['id']) ],
array_filter($array, fn($item) => $item['parent_id'] === $parent_id)
)
);
}
$result = extractChildren($input, 19);
print_r($result);
从内到外:
array_filter
删除“parent_id”不匹配的项目。array_map
从项目中获取“id”部分,并通过递归调用 extractChildren 来获取项目子项。array_merge
使用点差运算符使结果扁平化。
输出:
Array
(
[0] => 4
[1] => 10
[2] => 11
[3] => 5
[4] => 9
[5] => 13
[6] => 14
[7] => 17
[8] => 16
[9] => 3
[10] => 18
)
0赞
CBroe
10/10/2023
#2
可用于提取具有特定父项的所有项。然后你再次循环这些提取的项目,并以递归方式执行相同的操作。array_filter
请注意,此函数不仅返回子 ID,它仍然返回 [child, parent] 元组 - 这是它工作所必需的。但是,如果需要,您可以使用仅提取子 ID。array_column
$data = json_decode('[[1,0],[2,1],[3,16],[4,19],[5,19],[6,2],[7,2],[8,6],[9,19],[10,4],
[11,4],[12,2],[13,19],[14,13],[15,2],[16,19],[17,13],[18,19],[19,1]]');
function getRecursiveChildren($id, $items) {
$res = array_filter($items, function($item) use ($id) {
return $item[1] === $id;
});
foreach($res as $item) {
$res += getRecursiveChildren($item[0], $items);
}
return $res;
}
print_r(array_column(getRecursiveChildren(19, $data), 0));
结果:
Array
(
[0] => 4
[1] => 5
[2] => 9
[3] => 13
[4] => 16
[5] => 18
[6] => 10
[7] => 11
[8] => 14
[9] => 17
[10] => 3
)
评论
$items
$items
parent_id