简体   繁体   English

PHP - 连接2个对象

[英]PHP - Concatenating 2 objects

I have 2 objects. 我有2个物体。

Here is the output of the objects when I print them out with print_r method of the PHP. 这是使用PHP的print_r方法打印出来的对象的输出。

Oject #1; 对象#1;

stdClass Object ( [id] => 1 [portal_id] => 1 [name=> NEVZAT )

Object #2; 对象#2;

stdClass Object ( [surname] => YILMAZ)

I want to concatenate these 2 objects to each other so at the end of the process I need an Object which contains all of the variables of the 2 objects; 我想将这两个对象相互连接起来,所以在进程结束时我需要一个包含2个对象的所有变量的Object;

stdClass Object ( [id] => 1 [portal_id] => 1 [name=> NEVZAT [surname] => YILMAZ )

一种简单的方法是将对象临时转换为数组,合并这些数组,然后将结果数组转换回stdClass对象。

$merged = (object) array_merge((array) $object_a, (array) $object_b);

Just copy over the attributes like so: 只需复制属性如下:

// assume $o1 and $o2 are your objects
// we copy $o1 attributes to $o2
foreach ($o1 as $attr => $value) {
        $o2->{$attr} = $value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM