将flat PHP数组转换为多维数组

时间:2014-11-09 20:50:20

标签: php arrays multidimensional-array

我有以下数组结构:

[parents] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 1
                [created] => 2014-11-09 13:47:37
                [content] => This is a test discussion
                [status] => 1
                [parent] => 0
                [project_id] => 1
            )

        [1] => Array
            (
                [id] => 4
                [user_id] => 1
                [created] => 2014-11-09 13:52:02
                [content] => 456789
                [status] => 1
                [parent] => 0
                [project_id] => 1
            )

    )

[children] => Array
    (
        [0] => Array
            (
                [id] => 2
                [user_id] => 1
                [created] => 2014-11-09 13:47:53
                [content] => This is a test reply....
                [status] => 1
                [parent] => 1
                [project_id] => 1
            )

        [1] => Array
            (
                [id] => 3
                [user_id] => 1
                [created] => 2014-11-09 13:48:13
                [content] => This is a test reply....!!!
                [status] => 1
                [parent] => 1
                [project_id] => 1
            )

        [2] => Array
            (
                [id] => 5
                [user_id] => 1
                [created] => 2014-11-09 13:52:17
                [content] => 8765432
                [status] => 1
                [parent] => 4
                [project_id] => 1
            )

    )

我想将它们合并为父/子关系,如下所示:

[parents] => Array
    (
        [0] => Array
            (
                [id] => 1
                [user_id] => 1
                [created] => 2014-11-09 13:47:37
                [content] => This is a test discussion
                [status] => 1
                [parent] => 0
                [project_id] => 1
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 2
                                [user_id] => 1
                                [created] => 2014-11-09 13:47:53
                                [content] => This is a test reply....
                                [status] => 1
                                [parent] => 1
                                [project_id] => 1
                            )

                        [1] => Array
                            (
                                [id] => 3
                                [user_id] => 1
                                [created] => 2014-11-09 13:48:13
                                [content] => This is a test reply....!!!
                                [status] => 1
                                [parent] => 1
                                [project_id] => 1
                            )
                        )
            )
     )

我怎么能用PHP做到这一点?

1 个答案:

答案 0 :(得分:1)

假设id数组中的所有parents都是唯一的,您可以这样做:

// build a new array using parent's ID values as the key, 
// to simplify searching for parent IDs. 
foreach ($parents as $key => $value){
    $merged[$value['id']] = $value;
}

foreach ($children as $child){
    $parentID = $child['parent'];
    if (array_key_exists( $parentID, $merged )) {
        // add the child array to its parent's ['children'] value
        $merged[$parentID]['children'][] = $child;
    }
}

在生成的数组$merged中,每个父项的键将设置为其id,并且所有子项将嵌套在其对应的父项下。