2

Function/functions need to do the following:

  1. To accept an nested array tree
  2. Specifying witch sub array to flatten in my case I have ['children'] to flatten with parent items

Current tree:

Array
(
    [0] => stdClass Object
    (
        [id] => 1
        [comment_threads_id] => 1
        [users_id] => 1
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
            [0] => stdClass Object
            (
                [id] => 2
                [comment_threads_id] => 1
                [users_id] => 2
                [parent_id] => 1
                [message] => Lorem ipsum
                [created_at] => 2018-01-03 04:21:49
                [children] => Array
                (
                )

            )

            [1] => stdClass Object
            (
                [id] => 247
                [comment_threads_id] => 1
                [users_id] => 2
                [parent_id] => 1
                [message] => 
                [created_at] => 2018-01-03 04:30:07
                [children] => Array
                (
                )

            )

        )

    )

    [1] => stdClass Object
    (
        [id] => 3
        [comment_threads_id] => 1
        [users_id] => 1
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
            [0] => stdClass Object
            (
                [id] => 4
                [comment_threads_id] => 1
                [users_id] => 2
                [parent_id] => 3
                [message] => Lorem ipsum
                [created_at] => 2018-01-03 04:21:49
                [children] => Array
                (
                    [0] => stdClass Object
                    (
                        [id] => 250
                        [comment_threads_id] => 1
                        [users_id] => 2
                        [parent_id] => 4
                        [message] => 
                        [created_at] => 2018-01-03 05:03:09
                        [children] => Array
                        (
                        )

                    )

                    [1] => stdClass Object
                    (
                        [id] => 251
                        [comment_threads_id] => 1
                        [users_id] => 2
                        [parent_id] => 4
                        [message] => 
                        [created_at] => 2018-01-03 05:25:58
                        [children] => Array
                        (
                            [0] => stdClass Object
                            (
                                [id] => 252
                                [comment_threads_id] => 1
                                [users_id] => 2
                                [parent_id] => 251
                                [message] => 
                                [created_at] => 2018-01-03 05:05:09
                                [children] => Array
                                (
                                )

                            )
                        )

                    )

                )

            )

            [1] => stdClass Object
            (
                [id] => 248
                [comment_threads_id] => 1
                [users_id] => 2
                [parent_id] => 3
                [message] => 
                [created_at] => 2018-01-03 05:02:05
                [children] => Array
                (
                )

            )

        )

    )

    [2] => stdClass Object
    (
        [id] => 5
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
            [0] => stdClass Object
            (
                [id] => 6
                [comment_threads_id] => 1
                [users_id] => 2
                [parent_id] => 5
                [message] => Lorem ipsum
                [created_at] => 2018-01-03 04:21:49
                [children] => Array
                (
                    [0] => stdClass Object
                    (
                        [id] => 7
                        [comment_threads_id] => 1
                        [users_id] => 2
                        [parent_id] => 6
                        [message] => Lorem ipsum
                        [created_at] => 2018-01-03 04:21:49
                        [children] => Array
                        (
                            [0] => stdClass Object
                            (
                                [id] => 8
                                [comment_threads_id] => 1
                                [users_id] => 2
                                [parent_id] => 7
                                [message] => Lorem ipsum
                                [created_at] => 2018-01-03 04:21:49
                                [children] => Array
                                (
                                    [0] => stdClass Object
                                    (
                                        [id] => 9
                                        [comment_threads_id] => 1
                                        [users_id] => 1
                                        [parent_id] => 8
                                        [message] => Lorem ipsum
                                        [created_at] => 2018-01-03 04:21:49
                                        [children] => Array
                                        (
                                            [0] => stdClass Object
                                            (
                                                [id] => 10
                                                [comment_threads_id] => 1
                                                [users_id] => 2
                                                [parent_id] => 9
                                                [message] => Lorem ipsum
                                                [created_at] => 2018-01-03 04:21:49
                                                [children] => Array
                                                (
                                                    [0] => stdClass Object
                                                    (
                                                        [id] => 11
                                                        [comment_threads_id] => 1
                                                        [users_id] => 2
                                                        [parent_id] => 10
                                                        [message] => Lorem ipsum
                                                        [created_at] => 2018-01-03 04:21:49
                                                        [children] => Array
                                                        (
                                                            [0] => stdClass Object
                                                            (
                                                                [id] => 12
                                                                [comment_threads_id] => 1
                                                                [users_id] => 2
                                                                [parent_id] => 11
                                                                [message] => Lorem ipsum
                                                                [created_at] => 2018-01-03 04:21:49
                                                                [children] => Array
                                                                (
                                                                    [0] => stdClass Object
                                                                    (
                                                                        [id] => 13
                                                                        [comment_threads_id] => 1
                                                                        [users_id] => 2
                                                                        [parent_id] => 12
                                                                        [message] => Lorem ipsum
                                                                        [created_at] => 2018-01-03 04:21:49
                                                                        [children] => Array
                                                                        (
                                                                        )

                                                                    )

                                                                )

                                                            )

                                                        )

                                                    )

                                                )

                                            )

                                        )

                                    )

                                )

                            )

                        )

                    )

                )

            )

        )

    )

)

What should it look like:

Array
(
    [0] => stdClass Object
    (
        [id] => 1
        [comment_threads_id] => 1
        [users_id] => 1
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [1] => stdClass Object
    (
        [id] => 2
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 1
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [2] => stdClass Object
    (
        [id] => 247
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 1
        [message] => 
        [created_at] => 2018-01-03 04:30:07
        [children] => Array
        (
        )

    )

    [3] => stdClass Object
    (
        [id] => 3
        [comment_threads_id] => 1
        [users_id] => 1
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [4] => stdClass Object
    (
        [id] => 4
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 3
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [5] => stdClass Object
    (
        [id] => 250
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 4
        [message] => 
        [created_at] => 2018-01-03 05:03:09
        [children] => Array
        (
        )

    )

    [6] => stdClass Object
    (
        [id] => 251
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 4
        [message] => 
        [created_at] => 2018-01-03 05:25:58
        [children] => Array
        (           
        )

    )

    [7] => stdClass Object
    (
        [id] => 252
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 251
        [message] => 
        [created_at] => 2018-01-03 05:05:09
        [children] => Array
        (
        )

    )

    [8] => stdClass Object
    (
        [id] => 248
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 3
        [message] => 
        [created_at] => 2018-01-03 05:02:05
        [children] => Array
        (
        )

    )



    [9] => stdClass Object
    (
        [id] => 5
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 0
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (           
        )

    )

    [10] => stdClass Object
    (
        [id] => 6
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 5
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [11] => stdClass Object
    (
        [id] => 7
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 6
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [12] => stdClass Object
    (
        [id] => 8
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 7
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [13] => stdClass Object
    (
        [id] => 9
        [comment_threads_id] => 1
        [users_id] => 1
        [parent_id] => 8
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [14] => stdClass Object
    (
        [id] => 10
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 9
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [15] => stdClass Object
    (
        [id] => 11
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 10
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [16] => stdClass Object
    (
        [id] => 12
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 11
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

    [17] => stdClass Object
    (
        [id] => 13
        [comment_threads_id] => 1
        [users_id] => 2
        [parent_id] => 12
        [message] => Lorem ipsum
        [created_at] => 2018-01-03 04:21:49
        [children] => Array
        (
        )

    )

)

So for each parent object I need to extract it's children array(if It's not empty) and for each of child object(if It's not empty) I need to extract It's array and so on and so on.

It needs to keep the same ID order when extracted in the final array (this is the goal).

I've tried few similar methods with flattening arrays with some PHP SPL iterator functions and without them but I can't get the right order, in the end array there are missing objects and so far I am also looking to get the best performances out of this in case that there are 10.000 and more nested rows to handle.

1 Answer 1

5

A recursive function to flatten an array of objects like this basically needs to do two things.

  1. For each object in the array, separate its children and then add it to the output array.
  2. Recursively flatten the array of children and add the result to the output array.

Here's an example that works that way.

function flatten($input, $key) {
    $output = [];

    // For each object in the array
    foreach ($input as $object) {

        // separate its children
        $children = isset($object->$key) ? $object->$key : [];
        $object->$key = [];

        // and add it to the output array
        $output[] = $object;

        // Recursively flatten the array of children
        $children = flatten($children, $key);

        //  and add the result to the output array
        foreach ($children as $child) {
            $output[] = $child;
        }
    }
    return $output;
}

I'm not 100% certain what you mean by "keep the same ID order", but if you mean what I think you do, this will do that.

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

9 Comments

Don't you need to pass the array you are adding to?...or append the results of the recursion?
@Ctznkane525 $output[] = $child; appends the results of the recursion.
Missed that...my bad
No worries. This is not the most efficient way to flatten, but it ensures the "same ID order" (if I understood that correctly).
This is the solution I was looking for, seems like I was doing it wrong at one point. The ID order stays as planned. Thank you
|

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.