What Is the Best Method to Merge Two PHP Objects

What is the best method to merge two PHP objects?

If your objects only contain fields (no methods), this works:

$obj_merged = (object) array_merge((array) $obj1, (array) $obj2);

This actually also works when objects have methods. (tested with PHP 5.3 and 5.6)

Merge two objects in php

$info = yourstuff;
$arrContact = (array) $info->contact;
$arrList = (array) $info->lists[0];
$merged = array_merge($arrContact, $arrList);
var_dump($merged, 'have fun');

Quite trivial ;)

Merging two php objects or arrays on specific value

Thanks Wazelin and nerdlyist I was looking at this problem wrong way, this was SQL not PHP problem, I just edited mine query and I got results that I need, and once again thanks Wazelin for pointing me in right direction, left join did great job, ill post code so someone with similar issue can find solution faster ;)

SELECT * FROM users LEFT JOIN service ON users.id=service.userID WHERE users.grupa = 'service'

How to merge all property values of two objects in PHP

What you want to achieve is a sum of the properties. A merge would overwrite the values. There is no built-in PHP function to do this with objects.

But you can use a simple helper function where you could put in as many objects as you like to sum up the public properties.

function sumObjects(...$objects): object
{
$result = [];
foreach($objects as $object) {
foreach (get_object_vars($object) as $key => $value) {
isset($result[$key]) ? $result[$key] += $value : $result[$key] = $value;
}
}
return (object)$result;
}

$sumObject = sumObjects($objectA, $objectB);
stdClass Object
(
[a] => 3
[b] => 3
[d] => 3
)

Merge two objects in php with slightly different properties

How about this?

<?php

$grades = [];

foreach ($grade_records as $record) {
$grade_object = new stdClass();
$grade_object->fields = array(
'id' => $record->id,
'grade' => $record->grade,
'user_id' => $record->user_id,
'course_id' => $record->course_id,
);

$grades[$record->id] = $grade_object;
}

$attendance = [];

foreach ($attendance_records as $record) {
$attendance_object = new stdClass();
$attendance_object->fields = array(
'id' => $record->id,
'attendance' => $record->attendance,
'user_id' => $record->user_id,
'course_id' => $record->course_id
);

$attendance[$record->id] = $attendance_object;
}

$identifiers = array_intersect(
array_keys($grades),
array_keys($attendance)
);

$final_array = [];

foreach ($identifiers as $id) {
$object = new \stdClass();
$object->fields = \array_merge(
$grades[$id]->fields,
$attendance[$id]->fields
);

$final_array[] = $object;
}

How do I merge two objects?

If $bar has all of $foo's properties, then it is a Foo. Thus, your Bar class should extend (inherit from) Foo. (Or vice versa, if I got your names confused!)

If you don't want to use inheritance for some reason, you can keep one object inside the other, and redirect calls to the other's properties from the outer object dynamically via magic methods like __get() and __set (see here)



Related Topics



Leave a reply



Submit