How can I merge two objects and sum the values of matching properties? I am hoping for a built in function in PHP, otherwise I am seeking an easy way of doing it.
See code under, where I have $objectA
and $objectB
which I want to become $obj_merged
.
$objectA = (object) [];
$objectA->a = 1;
$objectA->b = 1;
$objectA->d = 1;
$objectB = (object) [];
$objectB->a = 2;
$objectB->b = 2;
$objectB->d = 2;
$obj_merged = (object) [
'a' => 3,
'b' => 3,
'd' => 3
];