PHP:兄弟与孩子在循环中的多维数组

时间:2017-04-11 09:55:47

标签: php arrays

我正在尝试将数组的兄弟元素转换为父子格式。

将此特定格式嵌入图形格式的原因。

我尝试了几个数组函数,比如array_walk_recursive等,并且能够进行转换。但是,当我预见到第n级的场景时,它实际上是分手了。

因此,场景是将每个索引元素与下一个元素链接为父元素,并在嵌套版本中创建一个子元关系。 实际数组

Array
(
    [0] => Array
        (
            [name] => Student 1
            [parent] => null 
            [children] => Fail-1
        )

    [1] => Array
        (
            [name] => Student 2 
            [parent] => null
            [children] => Fail-2
        )

    [2] => Array
        (
            [name] => Student 3 
            [parent] => null
            [children] => Pass
        )

)

预期结果

          Array
          (
                [name] => Student 1
                [parent] => null 
                [children] => Array
                    (
                       [name] => Fail-1 
                       [parent] => Student 1
                       [children] => Array
                           (
                               [name] => Student 2 
                               [parent] => Fail-1 
                               [children] => Array
                                  (
                                      [name] => Fail-2 
                                      [parent] => Student 2 
                                      [children] => Array
                                          (
                                              [name] => Student 3 
                                              [parent] => Fail-2
                                              [children] => Array
                                                  (
                                                     [name] => Pass 
                                                     [parent] => null
                                                     [children] => null
                                                  )
                                          )
                                  )

                           )
                    )
            )

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西:

<?php
$input = [
    [
        'name' => 'Student 1',
        'parent' => NULL,
        'children' => 'Fail-1'
    ],
    [
        'name' => 'Student 2',
        'parent' => NULL,
        'children' => 'Fail-2'
    ],
    [
        'name' => 'Student 3',
        'parent' => NULL,
        'children' => 'Pass'
    ]
];

$output = [];
array_walk($input, function($entry) use (&$output) {
    $entry['children'] = [
        'name' => $entry['children'],
        'parent' => $entry['name'],
        'children' => null
    ];
    $output[] = $entry;
});

do {
    $toBeMoved = array_pop($output);
    $toBeMovedTo = &$output[count($output)-1];
    $toBeMoved['parent'] = $toBeMovedTo['children']['name'];
    $toBeMovedTo['children']['children'] = $toBeMoved;
} while (1 < count($output));
print_r($output);

上述代码的输出为:

Array
(
    [0] => Array
        (
            [name] => Student 1
            [parent] =>
            [children] => Array
                (
                    [name] => Fail-1
                    [parent] => Student 1
                    [children] => Array
                        (
                            [name] => Student 2
                            [parent] => Fail-1
                            [children] => Array
                                (
                                    [name] => Fail-2
                                    [parent] => Student 2
                                    [children] => Array
                                        (
                                            [name] => Student 3
                                            [parent] => Fail-2
                                            [children] => Array
                                                (
                                                    [name] => Pass
                                                    [parent] => Student 3
                                                    [children] =>
                                                )

                                        )

                                )

                        )

                )

        )

)

除了最后一个条目中的parent元素之外,这是您发布的“预期”输出。您为该元素提出的值null在我看来是不合逻辑的,这就是我忽略它的原因。但是,为该元素实现最终的异常赋值是微不足道的。