树(或层次结构)数组到PHP中的二维结构化数组

时间:2014-07-20 17:22:35

标签: php arrays json

如何将树状数组转换为二维线性数组?现在我仍然坚持这个问题。可能有人问这个问题,但现在我还没有找到合适的方法。

$str='[{"id":1},{"id":2,"children":[{"id":3,"children":[{"id":4}]},{"id":5,"children":[{"id":6},{"id":7,"children":[{"id":8}]}]},{"id":9},{"id":10}]},{"id":11,"children":[{"id":12}]}]'

当我使用json_decode()函数将其转换为数组时,这是我的json字符串。我得到了以下结果

    Array
    (
        [0] => stdClass Object
            (
                [id] => 1
            )
        [1] => stdClass Object
            (
                [id] => 2
                [children] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 3
                                [children] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [id] => 4
                                            )
                                    )
                            )
                        [1] => stdClass Object
                            (
                                [id] => 5
                                [children] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [id] => 6
                                            )
                                    )
                            )
                        [2] => stdClass Object
                            (
                                [id] => 7
                            )
                        [3] => stdClass Object
                            (
                                [id] => 8
                            )
                    )
            )
        [2] => stdClass Object
            (
                [id] => 9
                [children] => Array
                    (
                        [0] => stdClass Object
                            (
                                [id] => 10
                            )
                    )
            )
    )

这就像树结构,但我需要将其转换为二维数组,如

 Array
    (
        [0] => Array
            (
                [parent] => 0
                [id] => 1
            )

        [1] => Array
            (
                [parent] => 0
                [id] => 2
            )

        [2] => Array
            (
                [parent] => 2
                [id] => 3
            )

        [3] => Array
            (
                [parent] => 3
                [id] => 4
            )

        [4] => Array
            (
                [parent] => 2
                [id] => 5
            )

        [5] => Array
            (
                [parent] => 5
                [id] => 6
            )

        [6] => Array
            (
                [parent] => 2
                [id] => 7
            )

        [7] => Array
            (
                [parent] => 2
                [id] => 8
            )

        [8] => Array
            (
                [parent] => 0
                [id] => 9
            )

        [9] => Array
            (
                [parent] => 0
                [id] => 10
            )

    )

1 个答案:

答案 0 :(得分:1)

您需要编写recursive function和/或循环来将对象排序为类似于您指定的数组。

我首先要说的是,将来如果你能发布样本数据和预期的输出实际上与你想要的相符并期望会很好 - 因为我花了十分钟时间弄清楚为什么我的功能告诉我当你的例子说它应该是2,当你的JSON和你的示例数组实际上不同时,id 7的父级是id 5,这很烦人。

无论如何,这里的想法是:

  • Recursive function检查传递的数组中的子项。如果存在,请再次对其子项调用该函数。无论是否有孩子,都要将电流添加到输出数组中。
  • &$output = output variable passed by reference,因此您无需返回任何内容,无需全局通话即可访问输出变量。
  • $parent_id代表每次您对子项进行迭代时的父ID。在节点没有父节点的第一种情况下,声明$parent_id = 0将定义您的"默认"父母ID。否则,每次深入时都会传递父ID。
function checkForChildrenOtherwiseAddToArray(&$output, $array, $parent_id = 0) {
    // loop through all sub-arrays inside this instance (if any)
    foreach($array as $each) {
        // check for children
        if(isset($each->children)) {
            // go deeper, passing in children and parent ID
            checkForChildrenOtherwiseAddToArray($output, $each->children, $each->id);
        }
        // add current iteration to array as well
        $output[] = array(
            'parent' => $parent_id,
            'id' => $each->id
        );
    }
}

使用示例:

$your_array = json_decode($str);
$output = array();
checkForChildrenOtherwiseAddToArray($output, $your_array);

由于此函数的递归特性,这将为您提供看似无序的结果。要按ID for example排序,您可以使用usort()

usort($output, function($a, $b) {
    return $a['id'] - $b['id'];
});

...您的示例输出将是:

Array
(
    [0] => Array
        (
            [parent] => 0
            [id] => 1
        )

    [1] => Array
        (
            [parent] => 0
            [id] => 2
        )

    [2] => Array
        (
            [parent] => 2
            [id] => 3
        )

    [3] => Array
        (
            [parent] => 3
            [id] => 4
        )

    [4] => Array
        (
            [parent] => 2
            [id] => 5
        )

    [5] => Array
        (
            [parent] => 5
            [id] => 6
        )

    [6] => Array
        (
            [parent] => 5
            [id] => 7
        )

    [7] => Array
        (
            [parent] => 7
            [id] => 8
        )

    [8] => Array
        (
            [parent] => 2
            [id] => 9
        )

    [9] => Array
        (
            [parent] => 2
            [id] => 10
        )

    [10] => Array
        (
            [parent] => 0
            [id] => 11
        )

    [11] => Array
        (
            [parent] => 11
            [id] => 12
        )

)