首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找数组中的所有后代

问查找数组中的所有后代
EN

Stack Overflow用户
提问于 2021-08-12 19:47:18
回答 1查看 55关注 0票数 2

乍一看,这是一项简单的任务,但当我试图解决它时,我变得困惑了。

我有PHP数组,其中键是顶级父I,值是子I。有些子ID有自己的子ID。孩子可以有自己的孩子,诸如此类。我的目标是获得一个新的数组,其中所有顶级父is都有其所有级别的子代(子代、孙代、曾孙等)。

代码语言:javascript
复制
<?php 
// Source array 
$source = array(
    '24' => array(
        '23',
        '22'
    ),
    '25' => false,
    '22' => array(
        '21'
    ),
    '21' => array(
        '20'
    ),
    '20' => array(
        '19'
    ),
    '19' => array(
        '18'
    ),
    '18' => array(
        '17'
    )
);

// Result array I need to achieve
$result = array(
    '24' => array(
        '23',
        '22',
        '21',
        '20',
        '19',
        '18',
        '17'
    ),
    '22' => array(
        '21',
        '20',
        '19',
        '18',
        '17'
    ),
    '21' => array(
        '20',
        '19',
        '18',
        '17'
    ),
    '20' => array(
        '19',
        '18',
        '17'
    ),
    '19' => array(
        '18',
        '17'
    ),
    '18' => array(
        '17'
    )
);


?>

我的尝试是下一步:

代码语言:javascript
复制
<?php 

function get_all_descendants($source)
{
    $result = $source;
    foreach ($source as $parent_id => $children) {
        foreach ($children as $op) {
            if (isset($source[$op])) {
                foreach ($source[$op] as $bottom_level_id) {
                    $result[$parent_id][] = $bottom_level_id;
                }
            }
        }
    }

    return $result;
}

var_dump( get_all_descendants( $source ) );

array(7) {
  [24]=>
  array(3) {
    [0]=>
    string(2) "23"
    [1]=>
    string(2) "22"
    [2]=>
    string(2) "21"
  }
  [25]=>
  bool(false)
  [22]=>
  array(2) {
    [0]=>
    string(2) "21"
    [1]=>
    string(2) "20"
  }
  [21]=>
  array(2) {
    [0]=>
    string(2) "20"
    [1]=>
    string(2) "19"
  }
  [20]=>
  array(2) {
    [0]=>
    string(2) "19"
    [1]=>
    string(2) "18"
  }
  [19]=>
  array(2) {
    [0]=>
    string(2) "18"
    [1]=>
    string(2) "17"
  }
  [18]=>
  array(1) {
    [0]=>
    string(2) "17"
  }
}

?>

但是这个函数不能找到所有的子代,似乎应该有递归,但我不能想象如何编写它。通常,我在过去使用递归编写过不同的函数,但在这种情况下,我打破了我的想法。)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-12 20:07:14

您可以使用堆栈或递归,然后深入研究结构:

代码语言:javascript
复制
<?php

function find_all_descendents($arr) {
    $all_results = [];

    foreach ($arr as $k => $v) {
        $curr_result = [];
        
        for ($stack = [$k]; count($stack);) {
            $el = array_pop($stack);

            if (array_key_exists($el, $arr) && is_array($arr[$el])) {
                foreach ($arr[$el] as $child) {
                    $curr_result []= $child;
                    $stack []= $child;
                }
            }
        }

        if (count($curr_result)) {
            $all_results[$k] = $curr_result;
        }
    }

    return $all_results;
}

$source = [
    '24' => ['23', '22'],
    '25' => false,
    '22' => ['21'],
    '21' => ['20'],
    '20' => ['19'],
    '19' => ['18'],
    '18' => ['17']
];
var_dump(find_all_descendents($source));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68763376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档