Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4059204
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:10:42+00:00 2026-05-20T15:10:42+00:00

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

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:10:42+00:00Added an answer on May 20, 2026 at 3:10 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, this may be a really stupid question, but I'm obviously missing something here.
This question is a bit of a two-parter. First, the title question. Here's what
This is really two questions one leading into the other. Firstly what does 'Permission
This is two questions in one really... First, I have a list of 'tickets'.
There are some related questions here and here , but they didn't really give
I have two questions... Here is a really simple example script which causes an
So two questions here: If I use <input type=button onclick=validate()> then the enter key
In my two recent questions here and here I laid out my problem a
Here is the question: write a method that swaps two variables. These two variables
I have a question, here are two classes below: class Base{ public: virtual void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.