Compare Object Properties and Show Diff in PHP

compare object properties and show diff in PHP

Something like the following, which iterates through and does a recursive diff is the item in the array is itself an array could work:

Des similar work to array_diff, but it does a check to see if it is an array first (is_array) and if so, sets the diff for that key to be the diff for that array. Repeats recursively.

function recursive_array_diff($a1, $a2) { 
$r = array();
foreach ($a1 as $k => $v) {
if (array_key_exists($k, $a2)) {
if (is_array($v)) {
$rad = recursive_array_diff($v, $a2[$k]);
if (count($rad)) { $r[$k] = $rad; }
} else {
if ($v != $a2[$k]) {
$r[$k] = $v;
}
}
} else {
$r[$k] = $v;
}
}
return $r;
}

It then works like this:

$obj1 = new StdClass; $obj1->prop = array(1,2);
$obj2 = new StdClass; $obj2->prop = array(1,3);
print_r(recursive_array_diff((array)$obj1, (array)$obj2));

/* Output:
Array
(
[prop] => Array
(
[1] => 2
)
)
*/

What's the fastest way to compare two objects in PHP?

The only way to access the all (public, protected and private) properties of an object without modifying it is through reflection. You could, if you wanted to, clone the object before it's modified and then compare them by using the reflection functions to loop through the different properties of one of the two objects and comparing values. That would effectively tell you what properties have changed.

If all you care to see is whether or not the objects have changed, you can use the == or === operators. Have a look at the comparing objects section of PHP's documentation.

That said, wouldn't just be easier to keep track of what you've changed as you change them?

PHP compare two objects with same properties but different properties values, get non-matching values

There are built in functions for that. You do need to cast the objects to an array for this to work though (which is an array of all public properties of the object). Assuming your variable is called $var:

$a1 = (array)$var[0];
$a2 = (array)$var[1];
$inBoth = array_intersect_assoc($a1, $a2);
$onlyInFirst = array_diff_assoc($a1, $a2);
$onlyInSecond = array_diff_assoc($a2, $a1);

Objects comparison in PHP

PHP compares sequentially (in the order of declaration) the object properties and stops at the first inequal property found. This behavior is not documented, so there's not much to be said about it, sadly, other than looking at the source of PHP.

Not documented is usually a synonym of "don't rely on it".

PHP get difference of two arrays of objects

This is exactly what array_udiff is for. Write a function that compares two objects the way you would like, then tell array_udiff to use that function. Something like this:

function compare_objects($obj_a, $obj_b) {
return $obj_a->id - $obj_b->id;
}

$diff = array_udiff($first_array, $second_array, 'compare_objects');

Or, if you're using PHP >= 5.3 you can just use an anonymous function instead of declaring a function:

$diff = array_udiff($first_array, $second_array,
function ($obj_a, $obj_b) {
return $obj_a->id - $obj_b->id;
}
);

Compare two StdClass objects with PHP

There are two methods for comparing objects. Using the === operator returns true if the objects you are comparing are the same instance of the same class. i.e. They are the same object.

Using the == operator returns true if all the properties of both objects are equal, and the objects are instances of the same class.

You're comparing two different objects, so === will returns false, but == should return true.

See https://3v4l.org/UdFFT

Reference: https://www.php.net/manual/en/language.oop5.object-comparison.php



Related Topics



Leave a reply



Submit