php

How to merge objects in PHP

Although there is no built-in function, there are several ways to merge objects in PHP. For example, a new object can be created by adding the properties of two or more objects using a loop. Alternatively, the required objects can be converted into arrays, which can be merged by using array_merge() or array_merge_recursively(), and then reconverted to an object.

In this tutorial, we will show you how to use the above methods to merge objects in PHP.

Example 1: Merge objects using a for loop

To follow this tutorial, create a PHP file with the following script. Here, an Account class is declared. Account contains a display() method for printing the assigned object values of the class. The custom Merge_Object() function is declared to merge two objects of the Account class using a for loop. Two for loops are used to merge the values of the objects and store them in another object of the Account class. The $acc_obj1 object is used to assign three property values of the Account class, and the $acc_obj2 object is used to assign two property values. These variables will be passed as arguments to the Merge_Object() function.

<?php

//Define class  
class Account {
   
    public function display($object)
    {
        echo "<b> Name: </b>".$object->name."<br />";
        echo "<b> Email: </b>".$object->email."<br />";
        echo "<b> Account Type: </b>".$object->acc_type."<br />";
        echo "<b> Openning Balance: </b>".$object->openning_balance."<br />";
        echo "<b> Current Balance: </b>".$object->current_balance;
    }

}
 
 
//Define function to merge objects
function Merge_Object($object1, $object2) {
 
    //Create a new object of the class
    $merge_object = new Account();
 
    //Assign the values of the first object into new object
    foreach($object1 as $property => $value) {
        $merge_object->$property = $value;
    }

    //Append the values of the second object into new object
    foreach($object2 as $property => $value) {
        $merge_object->$property = $value;
    }
 
    echo "<b> The values of the merged object: </b><br/><br/>";

    //Call the function to print the values of merged object
    $merge_object->display($merge_object);
}

//Create the first object and assign property values
$acc_obj1 = new Account();
$acc_obj1->name = "Mahmudul Ahsan";
$acc_obj1->email = "[email protected]";
$acc_obj1->acc_type = "Saving";

//Create the second object and assign property values
$acc_obj2 = new Account();
$acc_obj2->openning_balance = 40000;
$acc_obj2->current_balance = 50000;

//Call the function to merge both objects
Merge_Object($acc_obj1, $acc_obj2);
 
?>

Output:

The following output will be produced after running the above script from the webserver. The output shows the property values of the merged object variable.

Example 2: Merge objects using the array_merge() function

The array_merge() function is used to merge two or more arrays. To follow along with this example, create a PHP file with the following script. Here, the Account class is used to print the values of the merged object. The objects $acc_obj1 and $acc_obj2 are created in the script. Three property values are assigned using $acc_obj1 and two property values are assigned using $acc_obj2. These two objects are converted into two arrays and passed as arguments of the array_merge() function. The output of the array_merge() function is converted into an object to obtain $merge_object. Next, $merge_object is passed as an argument to the display() method of the Account class.

<?php

//Define class  
class Account {
   
    public function display($object)
    {
        echo "<b> Name: </b>".$object->name."<br />";
        echo "<b> Email: </b>".$object->email."<br />";
        echo "<b> Account Type: </b>".$object->acc_type."<br />";
        echo "<b> Openning Balance: </b>".$object->openning_balance."<br />";
        echo "<b> Current Balance: </b>".$object->current_balance;
    }

}
 
 
//Create the first object and assign property values
$acc_obj1 = new Account();
$acc_obj1->name = "Ferzana Rahman";
$acc_obj1->email = "[email protected]";
$acc_obj1->acc_type = "Current";

//Create the second object and assign property values
$acc_obj2 = new Account();
$acc_obj2->openning_balance = 500000;
$acc_obj2->current_balance = 450000;

//Merge the objects using array_merge()
$merge_object = (object) array_merge((array) $acc_obj1, (array) $acc_obj2);

echo "<b> The values of the merged object: </b><br/><br/>";

foreach($merge_object as $property => $value) {
        echo $property. " => ". $value. "<br/>";
}
?>

Output:

The following output will be produced by running the above script from the webserver. The output shows the property values of the merged object variable.

Example 3: Merge objects using the array_merge_recursive() function

Objects can also be merged by using the built-in array_merge_recursive() function, which is used to merge nested arrays. Therefore, if any array contains another array inside it, then this function will return a merged array by merging the values of the nested array.

To follow along with this example, create a PHP file with the following script. Here, a Student class is defined to return the merged object by using the Merge_Objects() method. The objects $obj1 and $obj2 are declared to store the id and marks properties. The marks property contains another object for storing the values of the other properties. Next, $obj1 and $obj2 are passed as arguments to Merge_Objects(), which uses the array_merge_recursive() function.

<?php

//Define the class
class Student {
   
    //Merge the two objects using array_merge_recursive()
    public function Merge_Objects($object1, $object2)
    {
        $Obj1 = (array) $object1;
        $Obj2 = (array) $object2;
        $merged = array_merge_recursive($Obj1, $Obj2);
        return (object) $merged;
    }
}

//Define the object of the class
$student = new Student();

//Declare the first object
$obj1 = (object) [
    'id' => '423456',
    'marks' => (object) [
        'CSE101' => 87
    ]
];

//Declare the second object
$obj2 = (object) [
    'marks' => (object) [
        'CSE204' => 94,
        'CSE306' => 75
    ]
];

//Call the function to merge the first and the second objects
$merged_object = $student->Merge_Objects($obj1, $obj2);

//Print the values of the merged object
echo "<pre>";
print_r($merged_object);
echo "</pre>";

?>

Output:

The following output will be produced by running the above script from the webserver. The output shows the property values of the merged object variable.

Conclusion

In this tutorial, we show you three different ways to merge objects in PHP. In the first example, the objects are merged by using a for loop, and in the other two examples, built-in functions for merging arrays are used.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.