You are using it incorrectly. Specifically, you are assuming that it will always be called with an item from the first array and an item from the second array, in that order, as arguments. But PHP does not make any such guarantee.
Now you could certainly make your comparison detect the types of its arguments and act accordingly, but that's not the best idea because it's swimming against the current. array_udiff
is supposed to operate on similarly structured arrays, which these are not. Additionally, you don't really need to check each element of the first array against each element of the second to achieve your goal.
Here's how I would do it (borrowing a bit of your code):
$extractor = function($o) { return explode(' - ', $o->name)[0]; };
$idsFromNames = array_flip(array_map($extractor, $nameArray));
foreach ($idArray as $k => $o) {
if (!isset($idsFromNames[$o->id])) {
unset($idArray[$k]);
}
}