I want to compare an array of complex objects with an array of ids, with the expected result being an array of any object that did not have its id listed.
This sounds like a perfect use case for array_udiff
, but I couldn't get it work without some confusing hassle. To illustrate my problem with that function, here a boiled down example:
class Foo {
public $id;
public function __construct($id) {
$this->id = $id;
}
}
$foos = [new Foo(1), new Foo(2), new Foo(3), new Foo(4)];
$fooIds = [1, 2, 3, 4];
$diff = array_udiff($foos, $fooIds, function ($f, $i){ return $f->id - $i; });
print_r($diff); // expected result: an empty array
// actual result:
// Array
// (
// [1] => Foo Object
// (
// [id] => 2
// )
// )
// Object of class Foo could not be converted to int :11
It sounds to me like array_udiff
tries to do some type coercion between the elements of the arrays. I found no mention of this in the docs, and one question on SO seems to ask something similar, but doesn't have any answers. What I'd like to know:
- Why does
array_udiff
behave this way? If we can supply an arbitrary callback function, this kind of coercion seems completely unnecessary, and in my case even very unhelpful. - Is there a good way to work around this, or should I use a different of function given my general problem?