What is the difference between treating an object as an array in PHP and using array_merge() to combine them?

When treating an object as an array in PHP, you can access its properties as if they were array elements using square brackets. On the other hand, using array_merge() function combines two or more arrays into a single array, preserving numeric keys and overwriting values with the same keys. If you want to merge an object's properties with an array, you need to first convert the object to an array using type casting or the get_object_vars() function before using array_merge().

// Treat object as an array
$obj = new stdClass();
$obj->key1 = 'value1';
$obj->key2 = 'value2';

// Convert object to array and merge with another array
$arr1 = (array) $obj;
$arr2 = ['key3' => 'value3', 'key4' => 'value4'];

$result = array_merge($arr1, $arr2);

print_r($result);