使用函数(php)递归获取第n个孩子的ID

问题描述 投票:0回答:2

我有一个包含两列的表格

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;
    }
?>

你能帮忙吗?!我的大脑被卡住了,我无法正常思考 谢谢

php mysqli
2个回答
0
投票

这将按照 item > child > child > item > item > item > child > item:

的顺序创建一个数组
$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
(
  [0] => 4
  [1] => 10
  [2] => 11
  [3] => 5
  [4] => 9
  [5] => 13
  [6] => 14
  [7] => 17
  [8] => 16
  [9] => 3
  [10] => 18
)

0
投票

您可以使用

array_filter
提取具有特定父项的所有项目。然后再次循环这些提取的项目,并递归地执行相同的操作。

请注意,此函数不仅仅返回子 ID,它仍然返回 [child,parent] 元组 - 这是它工作所必需的。但如果您愿意,您可以使用

array_column
仅提取子 ID。

$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
)
© www.soinside.com 2019 - 2024. All rights reserved.