3

i have the created the following object array in php

$treeData[] = (object) array(
    "name"=> "A",
    "children" => [
        [
            "name"=> "A1", 
            "children"=> [ 
                [ 
                    "name"=> "A1.1",
                    "children"=> [ 
                        [
                            "name"=> "A1.1.1",
                            "children"=> [ 
                                ....

I'm trying to push new values inside the children of A1.1.1 based on below condition:

foreach ($treeData as $value) {
    if ($value->name == 'A') {
        $value->name[][] = (object) array(
                                           "name"=> "ChildA", 
                                           "children"=> ""
                                    );
        break;
    }
}

But it's giving me an error

Expected result should match as below example:

$treeData[] = (object) array(
    "name"=> "A",
    "children" => [
        [
            "name"=> "A1",
            "children"=> [ [
                "name"=> "A1.1",
                "children"=> [ [
                    "name"=> "A1.1.1",
                    "children"=> [ 
                        [
                            "name"=> "ChildA",
                            "children"=> [ [ 
                                "name"=> "ChildA1"
                            ] ]
                        ],
                        [
                            "name"=> "ChildA",
                            "children"=> [ [
                                "name"=> "ChildA2"
                            ] ]
                        ],
                    ]
                ] ]
            ] ]
        ]
   ]);   

What I'm doing wrong here or any way to achieve this in different approach

The error I'm getting:

"Fatal error: Uncaught Error: [] operator not supported for strings "

6
  • Can you add the error you are getting for a more comprehensive understanding of its execution? :) Commented Jun 6, 2019 at 2:45
  • i have added the error Commented Jun 6, 2019 at 2:49
  • I think you are trying to force an array to an object attribute that is a string. You do not want to push it to $value->name but rather to $value->children if I am not mistaken Commented Jun 6, 2019 at 2:50
  • ok let me try it out Commented Jun 6, 2019 at 3:02
  • @DiogoSanto if i want to access children inside "name"=> "A1.1.1" how should i do it? i have written following foreach loop but give me error $testresult = []; foreach ($treeData as $value) { if($value->name == 'A'){ $testresult = $value->children->name; } } Commented Jun 6, 2019 at 3:21

1 Answer 1

2

First of all you getting the error as you do: $value->name[][]. Notice the name is a string so you ccannot use [] (array append operator) on it.

I would have take the recursive approach if I were you. Consider the following pseudo code:

function addChild($root, $addToName, $nameToAdd) {
    if ($root->name == $addToName)
        $root->children[] =  (object) array("name"=> $nameToAdd, "children"=> []);
    else 
        foreach($root->children as $child)
            addChild($child, $addToName, $nameToAdd);
}  

And now call is with: addChild($treeData, 'A', "ChildA")

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.