首页 > 解决方案 > PHP - 循环具有未知深度的数组的递归函数在两级后没有传递/保留父键

问题描述

我想要一些关于如何处理这个用例的建议:

我有以下未知深度的多维测试数组:

$entries =
[
    [
        'id' => 'Foo',
        'parent' => 'root',
        'logic_rules' => []
    ],
    [
        'id' => 'Bar',
        'parent' => 'root',
        'logic_rules' => [],
        'children' => [
            [
                'id' => 'Foobar',
                'parent' => 'Bar',
                'logic_rules' => [],
                'children' => [
                    [
                        'id' => 'Foobar2',
                        'parent' => 'Foobar',
                        'logic_rules' => [],
                        'children' => [
                            [
                                'id' => 'Foobar3',
                                'parent' => 'Foobar2',
                                'logic_rules' => []
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
];

我尝试使用这个递归函数遍历数组以执行一些逻辑(从示例中排除):

function traverse(array $entries, array &$result = [])
{
    foreach ($entries as $k => $value) {
        // If `logic_rules` exists proceed executing the logic
        // and store the result in the same `parent -> id` position
        if (array_key_exists('logic_rules', $value)) {
            $result[$value['parent']][$value['id']]['logic_rules'] = time(); // some logic
        }

        // Re-loop if is parent
        if (array_key_exists('children', $value)) {
            traverse($value['children'], $result);
        }
    }

    return $result;
}

的输出traverse($entries)是:

Array
(
    [root] => Array
        (
            [Foo] => Array
                (
                    [logic_rules] => 1603091236
                )

            [Bar] => Array
                (
                    [logic_rules] => 1603091236
                )

        )

    [Bar] => Array
        (
            [Foobar] => Array
                (
                    [logic_rules] => 1603091236
                )

        )

    [Foobar] => Array
        (
            [Foobar2] => Array
                (
                    [logic_rules] => 1603091236
                )

        )

    [Foobar2] => Array
        (
            [Foobar3] => Array
                (
                    [logic_rules] => 1603091236
                )

        )

)

但我希望这样:

Array
(
    [root] => Array
        (
            [Foo] => Array
                (
                    [logic_rules] => 1603091236
                )

            [Bar] => Array
                (
                    [logic_rules] => 1603091236

                    [Foobar] => Array
                        (
                            [logic_rules] => 1603091236

                            [Foobar2] => Array
                                (
                                    [logic_rules] => 1603091236

                                    [Foobar3] => Array
                                        (
                                            [logic_rules] => 1603091236
                                        )
                                )
                        )
                )

        )
)

似乎它正在跳过它的祖先。对此有何建议?

标签: phparraysrecursiontreetraversal

解决方案


解决方案是,refer to the results parent因此以下深度将推动它。

看看这一行:

traverse($value['children'], $result[$value['parent']]);

此代码将为您工作:

function traverse (array $entries, array &$result = []) {
  foreach ($entries as $value) {
    
      // add your logical stuff here
      $result[$value['parent']][$value['id']] = array(
        'logical_rules' => time()
      );

    if (array_key_exists('children', $value)) {
      traverse($value['children'], $result[$value['parent']]);
    }
  }
  return $result;
}

推荐阅读