How can I find an entry by object property from an array of objects?

How can I find an entry by object property from an array of objects?

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

Stefan Bogdanescu

Founder & Senior Architect

Finding an Entry by Object Property in an Array

When working with arrays of stdClass objects in PHP, locating a specific entry based on one of its properties is a common task. The scenario you described involves an array where each element is an object containing properties like ID and name. You have an integer variable $v, and your goal is to find the array index or the full object where the ID property matches $v.

The most direct approach is to iterate through the array and compare the target property of each object against your search value. However, manual iteration using a foreach loop often feels clunky if you only need the index or the specific object. PHP provides built-in functions that abstract this logic away, making the code more concise and readable.

Using Built-in Search Functions

The standard function for this operation is array_column(), but it works best when dealing with associative arrays where keys match property names. Since your data consists of stdClass objects, array_column() isn't the immediate solution unless you convert the structure first. A more robust native alternative is to use iterator_to_array combined with logic, or simply rely on a simple loop if performance isn't critical for small datasets.

However, there is a specific utility often overlooked: checking if a property exists and matching it. If you need just the index, a simple foreach loop is perfectly acceptable. But if you want to extract the object directly, consider using array_values() combined with a filter function if you are on PHP 5.4 or higher, though even simpler methods exist for raw arrays.

A cleaner native way without loops involves casting or mapping, but let's look at the most practical pattern used in modern PHP:

$v = 420; // The target ID

foreach ($array as $index => $item) {
    if ($item->ID == $v) {
        $foundIndex = $index;
        $foundObject = $item;
        break;
    }
}

This approach is explicit and easy to debug. It stops immediately once the match is found, which is efficient for sparse arrays.

Leveraging Functional Programming Styles

If you prefer a functional style or are using PHP 5.4+, you can use array_filter() with an anonymous function. This returns a new array containing all entries where the condition is true. If you expect only one result, you can then extract the first element.

$foundObjects = array_filter($array, function ($item) use ($v) {
    return $item->ID == $v;
});

// Extract the single matched object if it exists
if (count($foundObjects) > 0) {
    $result = reset($foundObjects);
} else {
    // Handle case where no entry was found
    $result = null; 
}

This method is verbose but avoids foreach syntax, which some developers find cleaner. It also naturally handles the case where multiple entries might share the same ID, returning all of them rather than stopping at the first match.

Performance Considerations and Trade-offs

When deciding between a manual loop and array_filter, consider the size of your dataset. For large arrays (thousands or millions of elements), performance matters. A linear search is O(n) regardless of the method used. However, if you find yourself doing this lookup frequently, maintaining an associative array keyed by ID might be a better architectural choice than searching a parallel array of objects every time.

// Create a map for fast lookups
$map = [];
foreach ($array as $item) {
    $map[$item->ID] = $item;
}

// Instant retrieval in O(1) time complexity
if (isset($map[$v])) {
    $result = $map[$v];
}

This trade-off involves extra memory for the map, but it saves CPU cycles during repeated lookups. If your data changes often, you must rebuild the map whenever the array is modified.

Handling stdClass Properties Safely

One potential pitfall with stdClass objects is that properties can be accessed dynamically without prior existence checks. While $item->ID works if the property exists, accessing non-existent properties returns null, which can cause issues in strict contexts or type-checking scenarios. Always ensure the property exists before comparing, especially if your data source might contain incomplete records.

if (isset($item->ID) && $item->ID == $v) {
    // Safe comparison
}

By checking isset(), you prevent potential notices or unexpected behavior when the ID property is missing from an object in the array. This defensive programming practice ensures your script remains robust even with inconsistent input data.

Our network