haku Posted October 30, 2008 Share Posted October 30, 2008 I have two objects. I want to combine them into one object. I tried array merge, and it didn't work. Does anybody know if/how this is possible? Quote Link to comment Share on other sites More sharing options...
samshel Posted October 30, 2008 Share Posted October 30, 2008 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 Quote Link to comment Share on other sites More sharing options...
haku Posted October 30, 2008 Author Share Posted October 30, 2008 They are not part of a class. Object 1 contains: $obj1->filename $obj1->path Object 2 contains: $obj2->uploaded_by $obj2->uploaded_on I want to combine them because the data goes together, and should all be part of one object, not two. Quote Link to comment Share on other sites More sharing options...
samshel Posted October 30, 2008 Share Posted October 30, 2008 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); Quote Link to comment Share on other sites More sharing options...
feidakila Posted October 30, 2008 Share Posted October 30, 2008 As samshel says you should have a parent class with all common properties for every child, and each child has its own properties declared in it. Quote Link to comment Share on other sites More sharing options...
haku Posted November 3, 2008 Author Share Posted November 3, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.