Merge two PHP stdClass() Objects

You, PHPCode snipped
Back

Below code snipped is to merge two PHP objects. This many be useful in numerous way. I'm not sure is there any other great way to do this, nwya dig in to code ;o)

<?php
$foo1 = new stdClass();
$foo2 = new stdClass();
$foo3 = new stdClass();

$foo1->one = "1";
$foo2->two = "2";

foreach ( $foo1 as $key => $value ) {
  $foo3->$key = $value;
}

foreach ( $foo2 as $key => $value ) {
  $foo3->$key = $value;
}

var_dump($foo3);
?>
© Heshan Wanigasooriya.RSS

🍪 This site does not track you.