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 "
$value->namebut rather to$value->childrenif I am not mistaken