Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Concatenating 2 objects

Tags:

php

I have 2 objects.

Here is the output of the objects when I print them out with print_r method of the PHP.

Oject #1;

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

Object #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;

stdClass Object ( [id] => 1 [portal_id] => 1 [name=> NEVZAT [surname] => YILMAZ )
like image 834
WhoSayIn Avatar asked Dec 03 '22 03:12

WhoSayIn


1 Answers

A simple way would be to temporarily cast the objects to arrays, merge those arrays, then case the resulting array back to a stdClass object.

$merged = (object) array_merge((array) $object_a, (array) $object_b);
like image 125
salathe Avatar answered Dec 27 '22 12:12

salathe