Check if an object exists in an array

Check if an object exists in an array

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

Stefan Bogdanescu

Founder & Senior Architect

Check if an Object Exists in an Array by ID Only

When working with collections of data, you often encounter scenarios where object structures are slightly inconsistent but share a unique identifier. Consider a situation where you have two arrays: one containing full person records and another containing partial or differently formatted entries. Both might represent the same entity, but one has language set to 'German' while the other uses 'de', perhaps due to normalization or user input variance.

If your goal is simply to determine whether an object in the second array corresponds to an entry in the first based solely on the id, you don't need a complex equality check that compares every property. Standard array functions like in_array() often fall short here because they perform strict value comparisons. They will fail if the arrays contain objects or associative arrays, as PHP checks for exact matches of keys and values.

To solve this efficiently, you should isolate the specific attribute that defines uniqueness—in this case, the id. The most robust approach in modern PHP is to extract the IDs from the master array into a separate collection and then check for membership using an identity operator or a specialized function.

Here is how you can implement this logic cleanly:

// Assume $array1 contains the full records and $array2 contains the checks
$idsInArray1 = array_column($array1, 'id');

foreach ($array2 as $entry) {
    // Check if the ID exists in our extracted list
    if (in_array($entry['id'], $idsInArray1, true)) {
        // Object exists based on ID match
        // Other attributes like language or casing differences don't matter here
        echo "Found person with ID: {$entry['id']}\n";
    } else {
        // No matching record found in array 1
        echo "No record found for ID: {$entry['id']}\n";
    }
}

The key here is using array_column() to transform the multi-dimensional data into a flat list of identifiers. By passing the third argument true to in_array(), you ensure strict comparison, which prevents loose type juggling from causing unexpected matches (e.g., matching an integer ID with a float). This approach decouples the existence check from the structural differences between the two arrays.

If you are working within the Laravel ecosystem, there is an even more concise way to handle this using Eloquent collections or native helper methods that integrate seamlessly with your application's logic. You can leverage collect to create a collection from the first array and use the contains() method:

$collection = collect($array1);

foreach ($array2 as $entry) {
    if ($collection->contains('id', $entry['id'])) {
        // Exists
    }
}

This syntax is often preferred in Laravel projects because it reads like natural language and leverages the underlying optimization of collections. It also allows you to chain further queries or transformations immediately after finding the match without needing to break out of the scope.

Performance Considerations

While the above solutions are perfectly adequate for small to medium-sized datasets, they rely on linear search complexity ($O(n)$) within the in_array or contains operations if applied repeatedly. If you are iterating through a massive $array2 and checking against a large $array1, this can become a bottleneck.

In high-performance scenarios where the master list doesn't change frequently, it is worth investing in an initial lookup structure. Converting the IDs from $array1 into an associative array or using a SplFixedArray (in older PHP versions) can reduce the lookup time to constant $O(1)$ complexity. However, for most application logic, the readability and maintainability of the array_column approach outweigh the marginal performance gains unless you are dealing with millions of records.

Always verify that your data types are consistent before performing these checks. If $entry['id'] comes in as a string from a JSON API but $array1 stores integers, strict comparison will return false even if the values look identical. Normalizing the input type during the extraction phase ensures reliable existence checks without needing to handle edge cases inside your conditional logic.

Our network