Jump to content

Combining two objects


haku

Recommended Posts

Hi,

 

are these 2 objects of the same class? if not, then the fact that they are 2 different object means that they are supposed to be seperate and they belong to 2 different entities.

 

Sorry if i got it wrong, but could you please elaborate more on why u want to merge 2 objects?

 

Thanks

Samshel

Ideally if you want the data to be together, you should have a parent class which has all these member functions..but to answer your question..

you can convert all properties of the 2 objects into arrays first and then use array_merge

 

$arr1 = get_object_vars($obj1);
$arr2 = get_object_vars($obj2);
$arrFinal = array_merge($arr1, $arr2);

Ideally that would be the case. But it's a drupal setup I was working with, and the original object doesn't have the properties of the second object. But the function only returns one object, so the properties of the second object needed to be added into the first object.

 

I actually just ended up doing it manually:

 

$obj1->uploaded_by = $obj2->uploaded_by;
$obj1->uploaded_on = $obj2->uploaded_on;
return $obj1;

 

Not the cleanest method, but it worked fine.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.