Here's your problem, from the manual page:
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
You're returning a boolean (false
), because you're comparing a timestamp to an object. (int) false
is zero, so each and every object will be seen as equal, hence your $diff
Array is empty.
Change your callback function to:
$func = function(stdClass $a, stdClass $b)
{//don't pass by reference, it's quite dangerous
$replacer = time();
$buffer = $a;
$a = $replacer;
$result = $a === $b ? 0 : -1;//equal? return zero!
$a = $buffer;
return $result;
};
That'll work, but $diff
will now, obviously contain all objects. And your callback is a bit messy, consider:
$func = function(stdClass $a, stdClass $b)
{//don't pass by reference, it's quite dangerous
return $a === $b ? 0 : -1;//compare objects
//or, if you want to compare to time, still:
return time() === $b ? 0 : -1;
};
That's a hell of a lot cleaner, and shorter, is it not?
Note
You will have to return -1
in case the two objects don't equate. Returning 1
in such cases implies that the object you're comparing is already greater than the values you're comparing it to. In that case, PHP will just stop looking, and will simply assume the value is not present in the array you're comparing the first array to... ok, this is getting rather complicated. Take your example:
[$a, $c] compare to [$b, $c]:
$a === $b => false: returns 1
PHP assumes $a > $b, so $a > $c is implied, $a is pushed into $diff
$c === $b => false returns 1
PHP assumes $c > $b, and is pushed to $diff, it's never compared to the next elem...
Returning -1 on false, however:
[$a, $c] compare to [$b, $c]:
$a === $b => false: returns -1
PHP assumes $a < $b, so:
$a === $c => false: returns -1
No more elems left, push $a to $diff
$c === $b => false returns -1
PHP assumes $c < $b, moving on:
$c === $c => true returns 0
Match found, $c is NOT pushed to $diff