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

rmbxnbpk  于 2023-10-15  发布在  PHP
关注(0)|答案(2)|浏览(101)

我有一个包含两列idparent_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;
    }
?>

你能帮帮我吗?!我的脑子卡住了,我再也不能思考了谢谢

nmpmafwu

nmpmafwu1#

您可以使用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
)
vh0rcniy

vh0rcniy2#

这将按照 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_filter删除“parent_id”不匹配的项。
  • array_map从一个项目中获取'id'部分,并通过递归调用 extractChildren 来获取项目的子项。
  • array_merge与spread运算符一起使用,结果将被删除。

输出量:

Array
(
  [0] => 4
  [1] => 10
  [2] => 11
  [3] => 5
  [4] => 9
  [5] => 13
  [6] => 14
  [7] => 17
  [8] => 16
  [9] => 3
  [10] => 18
)

相关问题