如何合并两个或多个php对象?

时间:2018-06-13 18:04:03

标签: php object

我有2个或更多具有相同部分的php对象。每个部分都有对象。我想把这些对象组合在一起。由于每个部分具有相同的标题,因此在合并它们之前删除新对象的标题。我的代码没有保持正确的结构,并且正在向主对象添加不需要的级别“组件”。感觉像我错过了一些明显的东西,但我无法弄清楚如何添加没有'组件'级别的新对象。

对象1示例

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
            )
    )
)

对象2示例

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
            )
    )
)

当前代码

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

当前结果

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
            )
    )
)

期望的结果

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
            )
    )
)

2 个答案:

答案 0 :(得分:0)

可能这样的工作吗?

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};                         
        }                 
    }            
} 

基本上似乎问题是确定从属组件的密钥名称。您必须添加自己的错误检查。例如,我假设第二个组件只有一个对象,但可能不是这样。也许您需要确保密钥以字符串" component_name"开头,也许这只是一个占位符。您只需要根据数据结构进行调整。

答案 1 :(得分:0)

有时将对象组合起来很忙,因为我们希望在将对象合并为单个对象(父对象)后保持对象的定义 选中此link以获得更多信息。