0

I have the following array structure:

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

    )

I would like to merge them into a parent/child relationship so it looks like follows:

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

How could I go about doing that with PHP?

3
  • 1
    Did you googled array_merge ? Commented Nov 9, 2014 at 20:55
  • By iterating the children array and appending the results to the appropriate parent element - what have you tried so far? Commented Nov 9, 2014 at 20:56
  • I attempted to write something like that, but I got stuck when figuring how to add the child array to the parent array. Since I need to add it based on the parents id not the index. Commented Nov 9, 2014 at 21:02

1 Answer 1

1

Assuming that all ids are unique in your parents array, you can do this:

// 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;
    }
}

In the resulting array $merged, the key of each parent item will be set to its id, and all children will be nested under their corresponding parents.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.