I'm trying to compute difference of two arrays of objects with array_udiff()
. My object structure is complicated and I can not rely on quantitative properties, because those properties of objects in both arrays may have same values, but they are stored as different instances (it is expected behavior).
So, here is my question: is there any way to detect same instances in both arrays using reference detection?
What have I tried?
I've tried this:
<?php
header('Content-Type: text/plain; charset=utf-8');
$a = new stdClass;
$b = new stdClass;
$c = new stdClass;
$a->a = 123;
$b->b = 456;
$c->c = 789;
$x = [ $a, $c ];
$y = [ $b, $c ];
$func = function(stdClass &$a, stdClass &$b){
$replacer = time();
$buffer = $a;
$a = $replacer;
$result = $a === $b;
$a = $buffer;
return $result;
};
$diff = array_udiff($x, $y, $func);
print_r($diff);
?>
And got unsuccessful results, because if I try to replace value for $x
element, php will not remove reference from $y
.
I have same output for:
$func = function(stdClass &$a, stdClass &$b){
return $a === $b;
};
and for
$func = function(stdClass &$a, stdClass &$b){
return $a == $b;
};
It is an empty array.
Any suggestions?