PHP in_array object comparing?

PHP in_array object comparing?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Limitation of in_array for Objects in PHP

You cannot reliably use in_array() to check if a specific object instance already exists within an array, unless you explicitly provide a custom comparison logic. By default, in_array() relies on PHP's loose comparison rules (==). When comparing two objects, this behavior depends entirely on whether the class defines a magic method named __clone or, more critically in modern PHP versions, how the engine handles object identity versus value equality.

If you simply pass an instance to in_array() without arguments, it attempts to compare properties. However, if your objects do not implement __toString() and are distinct instances (different memory addresses), loose comparison often fails to recognize them as duplicates because their internal property states might be identical even though they are different $this references. If you need to check for identity—meaning "is this exact object instance already in the list?"—the standard approach is to use ===, but in_array() does not support strict comparison by default without an explicit second argument.

To solve this, you must leverage the third parameter of in_array(): $strict. Setting it to true forces the function to use the identity operator (===) instead of the loose equality operator (==). This is the most direct way to answer "is this exact object instance in the array?".

$objectList = [$objA, $objB];
$newObject = $objA; // A duplicate instance

// Returns true because we are checking for strict identity (same memory reference)
if (in_array($newObject, $objectList, true)) {
    echo "Object already exists.";
} else {
    echo "Adding new object.";
}

While this works perfectly for references, it often isn't what developers actually want. In most real-world scenarios, you are not trying to check if the exact same memory address is in an array; you want to know if an object with equivalent state already exists. For example, you might have two User objects where $user1->id === $user2->id. You want to treat them as duplicates based on that ID, not based on whether they are the exact same variable reference.

If your logic relies on property values rather than memory addresses, in_array() is insufficient because it cannot automatically know which properties constitute a "duplicate." In such cases, you have three main architectural paths: implement a custom __serialize and __unserialize strategy to control cloning behavior, define a __clone method carefully (though this usually affects copying rather than equality checks), or, most commonly, override the comparison logic entirely.

The most robust solution is to use array filtering with a custom closure that defines your own equality rules. This approach gives you granular control over what constitutes a duplicate. You can check if an object's primary key matches, or iterate through relevant properties to find a match.

$distinctObjects = [];

function isDuplicate($candidate, $collection) {
    foreach ($collection as $existing) {
        // Example: Check if the 'id' property matches
        if (is_object($candidate) && is_object($existing)) {
            if ($candidate->id === $existing->id) {
                return true;
            }
        }
    }
    return false;
}

foreach ($sourceArray as $obj) {
    if (!isDuplicate($obj, $distinctObjects)) {
        $distinctObjects[] = $obj;
    }
}

If you are working within the Laravel ecosystem, you might find that the array_unique() function also behaves similarly to in_array(). Without a callback or strict mode (which array_unique doesn't natively support for complex objects in older versions), it relies on string representation or identity. In recent PHP 8+ updates, array_unique with a custom callback is possible, offering another avenue for value-based deduplication.

Ultimately, the decision to use in_array() with strict mode versus writing a custom filter depends on your definition of "duplicate." If you need reference equality, strict in_array is efficient and clean. If you need value equality based on specific properties, you must write the loop yourself or create a specialized collection method that encapsulates this logic. This trade-off between convenience and control is central to handling complex data structures in PHP.

Our network