0

Two questions here really; why is this happening? And what can be done about it?

Sorry the question's so long but most of it is just print_r output!

Basically, I start with a flat array of tags ($tags) each with an id (array index), name and parent_id. I then recursively iterate over $tags and nest all child tags within their parent. (see below)

It works! (see below for output). But the problem I'm having is that my flat array of tags is being written to from within the function that does the nesting/recursion. (see below)

Flat array of tags:

Array
(
    [1] => stdClass Object
        (
            [name] => instruments
            [parent_id] => 0
        )

    [2] => stdClass Object
        (
            [name] => strings
            [parent_id] => 1
        )

    [3] => stdClass Object
        (
            [name] => violin
            [parent_id] => 2
        )

    [4] => stdClass Object
        (
            [name] => cello
            [parent_id] => 2
        )

    [5] => stdClass Object
        (
            [name] => woodwind
            [parent_id] => 1
        )

    [6] => stdClass Object
        (
            [name] => flute
            [parent_id] => 5
        )
)

This is the recursively called function to nest the child tags. The problem is inside the if: I assign $tag to $tree[$i] and then add the children property to it. This results in the children property being added to $tag. This is what I want to stop happening.

public function tags_to_tree($tags, $parent_id = 0)
{
    $tree = array();
    foreach($tags as $i => $tag)
    {
        // add this tag node and all children (depth-first recursive)
        if(intval($parent_id) === intval($tag->parent_id))
        {
            $tree[$i] = $tag;
            $tree[$i]->children = $this->tags_to_tree($tags, $i);
        }
    }
    return $tree;
}

Nested tags output:

Array
(
    [1] => stdClass Object
        (
            [name] => instruments
            [parent_id] => 0
            [children] => Array
                (
                    [2] => stdClass Object
                        (
                            [name] => strings
                            [parent_id] => 1
                            [children] => Array
                                (
                                    [3] => stdClass Object
                                        (
                                            [name] => violin
                                            [parent_id] => 2
                                        )

                                    [4] => stdClass Object
                                        (
                                            [name] => cello
                                            [parent_id] => 2
                                        )
                                )
                        )

                    [5] => stdClass Object
                        (
                            [name] => woodwind
                            [parent_id] => 1
                            [children] => Array
                                (
                                    [6] => stdClass Object
                                        (
                                            [name] => flute
                                            [parent_id] => 5
                                        )
                                )
                        )
                )
        )
)

What can I do differently when adding the children property to $tree[$i], or when assigning $tag to $tree[$i] to stop this from happening?

Thanks!

1 Answer 1

2

The flat array is an array of objects (references), even though you are putting the objects into a new array, it is still the same object you are moving around.

If you don't want to be editing the same reference, have a look at Object Cloning

i.e. Use:

$tree[$i] = clone $tag;
Sign up to request clarification or add additional context in comments.

1 Comment

amazing! such a long question for such a simple answer, thank you very much. tested and working :)

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.