I have 2 or more php objects that have the same sections. Each section has objects in it. I want to combine these objects together. Since each section has the same title I remove the title of the new object before merging them. My code isn't keeping the proper structure and is adding an unwanted level 'component' to the primary object. Feels like I am missing something obvious but I Can't figure out how to add the new object without the 'component' level.
Object 1 Example
stdClass Object(
[section_1] => stdClass Object
(
[title] => Production
[component_name_68] => stdClass Object
(
[title] => custom component title
[id] => 68
[type] => component_name_68
[subtotal] => 1127.50
[desc] => custom description
)
)
)
Object 2 Example
stdClass Object(
[section_2] => stdClass Object
(
[title] => Production
[component_name_69] => stdClass Object
(
[title] => custom component title2
[id] => 69
[type] => component_name_69
[subtotal] => 1985.50
[desc] => custom description2
)
)
)
Current Code
foreach($this->Details as $section1){
foreach($newinfo as $section2){
if($section1->title == $section2->title){
unset($section2->title);
$section1->{"component"} = $section2;
}
}
}
Current Result
stdClass Object(
[section_1] => stdClass Object
(
[title] => Production
[component_name_68] => stdClass Object
(
[title] => custom component title
[id] => 68
[type] => component_name_68
[subtotal] => 1127.50
[desc] => custom description
)
[component] => stdClass Object (
[component_name_69] => stdClass Object
(
[title] => custom component title2
[id] => 69
[type] => component_name_69
[subtotal] => 1985.50
[desc] => custom description2
)
)
)
Desired Result
stdClass Object(
[section_1] => stdClass Object
(
[title] => Production
[component_name_68] => stdClass Object
(
[title] => custom component title
[id] => 68
[type] => component_name_68
[subtotal] => 1127.50
[desc] => custom description
)
[component_name_69] => stdClass Object
(
[title] => custom component title2
[id] => 69
[type] => component_name_69
[subtotal] => 1985.50
[desc] => custom description2
)
)
)