PHP - 循环未知深度数组的递归函数不是 passing/persisting 两层后的父键

PHP - Recursive function to loop array with unknown depth is not passing/persisting the parent keys after two levels

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

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

$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
                                        )
                                )
                        )
                )

        )
)

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

解决方案是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;
}