递归遍历数组并将选定的子键合并到父

时间:2016-02-09 17:13:34

标签: php arrays recursion

我有这个数组,我检查孩子是否为特定键("hidden")是true。如果是,我需要将节点的id附加到父级密钥"contentId"

我有这个工作,但只对数组中的第一项。我怀疑递归是由return语句以某种方式打破的。有任何想法吗?也许我错过了array_walk_recursive解决方案?

function bubbleUp(&$tree){
    for ($i=0; $i < count($tree); $i++){
        if ( isset($tree[$i]['children']) && is_array($tree[$i]['children']) ) {
            $tree[$i]['contentId'] = [ $tree[$i]['id'] ];
            array_push($tree[$i]['contentId'], bubbleUp($tree[$i]['children']));
        } else {
            return reportBack($tree[$i]);
        }
    }
    return $tree;
}

function reportBack($node){
    if ( $node['hidden'] ) {
        return $node['id'];
    } else {
        return '';
    }
}

$tree = [
    [
        "name" => "Intro",
        "id" => 123,
        "hidden" => false,
        "children" => [[
            "name" => "foo",
            "id" => 452,
            "hidden" => true,
            "children" => [
                [
                    "name" => "bar",
                    "id" => 982,
                    "hidden" => true,
                ],
                [
                    "name" => "gru",
                    "id" => 239,
                    "hidden" => true,
                ]
            ]
        ]]
    ]
];
bubbleUp($tree);
echo '<pre><small>'; print_r($tree); echo '</small></pre>';

最终结果意味着:

$tree = [
    [
        "name" => "Intro",
        "id" => 123,
        "hidden" => false,
        "children" => [[
            "name" => "foo",
            "id" => 452,
            "hidden" => true,
            "children" => [
                [
                    "name" => "bar",
                    "id" => 982,
                    "hidden" => true,
                ],
                [
                    "name" => "gru",
                    "id" => 239,
                    "hidden" => true,
                ]
            ],
            "contentId" => [452, 982, 239]
        ]],
        "contentId" => [123, 452, 982, 239]
    ]
];

2 个答案:

答案 0 :(得分:1)

即将下班回家,所以当我回到家时,我会尝试回到这里,但我尝试了一个array_walk解决方案,尝试在此过程中尝试这一点很有趣。虽然我不相信它的工作方式与你想要的完全一样,但它可以作为任何在我回家之前看过这个问题的人的指南。

function recursiveSearch(&$value) {
    if (isset($value['children'])) {
        foreach ($value['children'] as &$child) {
            $id = recursiveSearch($child);
            $value['contentID'][] = $id;
        }
    } else {
        if (isset($value['hidden']) && $value['hidden'] == true) {
          return $value['id'];
        }
    }   
}

array_walk($tree, 'recursiveSearch');

要重新进行迭代,这个解决方案尚未完成,但我必须下班回家,这可能有助于其他人回答,或者如果没有提醒我,当我回到家时回到这里:p

到目前为止,这是指向它的链接:https://ideone.com/RHql33

使这一点变得困难的主要部分是您希望根据父级访问级别将内容ID附加到不同的位置。

即。如果父项为false,则立即节点应在该分支中附加内容ID,但其下的所有子节点也应附加到该位置。 (希望这是有道理的,我在谈论这类东西时很可怕哈哈o_o)

答案 1 :(得分:1)

感谢Virtual Pigeon的回答,这导致了这一点:

function recursiveSearch(&$value) {
    $value['contentID'][] = $value['id'];
    if (isset($value['children'])) {
        foreach ($value['children'] as &$child) {
            $id = recursiveSearch($child);
            if ( is_array($id) ) {
                $value['contentID'] = array_merge($value['contentID'], $id);
            } else {
                $value['contentID'][] = $id;
            }
        }
        return $value['contentID'];
    } else {
        if (isset($value['hidden']) && $value['hidden'] == true) {
            return $value['id'];
        }
    }
}