0

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
            )
    )
)
Mike Volmar
  • 1,927
  • 1
  • 22
  • 31
  • Possible duplicate of [What is the best method to merge two PHP objects?](https://stackoverflow.com/questions/455700/what-is-the-best-method-to-merge-two-php-objects) – Jeff Jun 13 '18 at 18:07
  • its not a duplicate since my question and example is not simply merging two flat objects into one. I'm trying to retain the sections from object1 and add object to those existing sections. The answer to that other question is not helping me. – Mike Volmar Jun 13 '18 at 18:10

2 Answers2

0

Might something like this work?

foreach($this->Details as $section1){
    foreach($newinfo as $section2){ 
        if($section1->title == $section2->title){               
            unset($section2->title);

            $components = get_object_vars($section2);
            // Check to see that only one key is present. Skip if more than one.
            if (count($components) > 1) {
              continue;
            }

            $component_keys = array_keys($components);
            $component_key = reset($component_keys);


            $section1->{$component_key} = $section2->{$component_key};                         
        }                 
    }            
} 

Basically it seems the issue is determining the key name for the subordinate component. You would have to add your own error checking. For example, I assume the second component will only have the one object, but that may not be true. Perhaps you need to ensure the key begins with the string "component_name", perhaps that is just a placeholder. You would just need to adapt this to your data structure.

greendemiurge
  • 509
  • 3
  • 11
  • Is there no object version of as in arrays? $section1[] = $section2; – Mike Volmar Jun 14 '18 at 14:32
  • That particular syntax wouldn't have an equivalent because every property and method in an object has to be named, whereas PHP arrays can have either named keys or sequentially numbered keys. Bracket notation grabs the next key number and assigns it, but there is no inherent "next" property name in an object. – greendemiurge Jun 14 '18 at 14:50
  • maybe the best thing for me to do is convert them to arrays instead – Mike Volmar Jun 14 '18 at 15:01
0

Combining Objects sometime is hectic job because we want to keep the object definintion after merging them into single object(parent) Check this link for more information.

Exception
  • 789
  • 4
  • 18