I conducted several tests comparing objects using the in_array function. This is my conclusion:
When you try to find the same instance of an object in an array (e.g. OP), then in_array can work with a logical set of strict comparison.
When you try to find an object of the same class, but with a different instance , in_array exhibits illogical behavior.
PHP.net has a great user comment about illogical extreme cases.
// Example array $array = array( 'egg' => true, 'cheese' => false, 'hair' => 765, 'goblins' => null, 'ogres' => 'no ogres allowed in this array' ); // Loose checking -- return values are in comments // First three make sense, last four do not in_array(null, $array); // true in_array(false, $array); // true in_array(765, $array); // true in_array(763, $array); // true in_array('egg', $array); // true in_array('hhh', $array); // true in_array(array(), $array); // true // Strict checking in_array(null, $array, true); // true in_array(false, $array, true); // true in_array(765, $array, true); // true in_array(763, $array, true); // false in_array('egg', $array, true); // false in_array('hhh', $array, true); // false in_array(array(), $array, true); // false
As you can see, without strict testing, in_array tests in_array not make sense.
We know from PHP.net that two objects are the same when strictly compared ( === ) if they belong to the same class + instance. Two objects are already the same in an arbitrary comparison ( == ) when they belong to the same class.
I wrote some tests with objects to see what happens.
$a = new stdClass(); $a->egg = true; $b = new stdClass(); $b->cheese = false; $c = new stdClass(); $c->hair = 765; $d = new stdClass(); $d->goblins = null; $e = new stdClass(); $e->ogres = 'no ogres allowed in this array'; $array2 = array($a, $b, $c, $d, $e); $e = new stdClass(); $e->egg = null; $f = new stdClass(); $f->egg = false; $g = new stdClass(); $g->egg = 765; $h = new stdClass(); $h->egg = 763; $i = new stdClass(); $i->egg = 'egg'; $j = new stdClass(); $j->egg = 'hhh'; $k = new stdClass(); $k->egg = array(); in_array($e, $array2, false); // false in_array($f, $array2, false); // false in_array($g, $array2, false); // true in_array($h, $array2, false); // true in_array($i, $array2, false); // true in_array($j, $array2, false); // true in_array($k, $array2, false); // false in_array($e, $array2, true); // false in_array($f, $array2, true); // false in_array($g, $array2, true); // false in_array($h, $array2, true); // false in_array($i, $array2, true); // false in_array($j, $array2, true); // false in_array($k, $array2, true); // false
In recent checks, in_array checks in_array results.
The reason is as follows. If you are trying to find an object with a specific value, you are forced to use arbitrary comparison (because when the class does not match, strict comparison always fails). But because of the types of PHP variables in recent tests, these checks are considered true, because the value is considered true. Also note that the key on the object is ignored during arbitrary comparisons.