What is the best method ...
What is the best method ...
Here are a few methods to merge two PHP objects:
The + operator can be used to merge two objects. It will add the properties of the right object to the left object, overwriting any properties with the same name:
$obj1 = (object) ['name' => 'John', 'age' => 30];
$obj2 = (object) ['city' => 'New York', 'age' => 35];
$merged = $obj1 + $obj2;
print_r($merged);
Output:
stdClass Object
(
[name] => John
[age] => 35
[city] => New York
)
You can convert the objects to arrays, merge the arrays, and then convert back to an object:
$obj1 = (object) ['name' => 'John', 'age' => 30];
$obj2 = (object) ['city' => 'New York', 'age' => 35];
...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào