How to access a property of an object (stdClass Object) member/element of an array?

How to access a property of an object (stdClass Object) member/element of an array?

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

Stefan Bogdanescu

Founder & Senior Architect

Accessing Properties in Arrays Containing stdClass Objects

When you encounter an array populated with stdClass objects, the syntax for accessing data changes fundamentally from what you are likely used to with associative arrays or native PHP classes. Your current attempt, $array['id'], fails because the top-level key [0] does not hold a string value like 'id'; instead, it holds an object instance. Attempting to use bracket notation directly on that first element expects an array structure, but you are dealing with an object graph.

To retrieve the id property (which has a value of 25 in your example), you must first access the object itself and then invoke its properties using either dot notation or curly braces. The most straightforward approach is:

echo $array[0]->id;

This line executes two distinct operations. First, $array[0] retrieves the stdClass instance stored at index 0. Second, the -> operator accesses the id property on that specific object instance. This pattern scales naturally if your array contains multiple entries:

echo $array[0]->fname; // Outputs: 4
echo $array[1]->time;  // Accessing the second item in the loop

If you prefer curly braces for accessing properties, as is common when dealing with dynamic property names or to avoid potential issues with reserved keywords, the syntax remains consistent:

echo $array[0]->{'id'};

While functionally identical in most contexts, dot notation is generally preferred for its readability. However, if you need to iterate over this data structure—for instance, looping through every item to render a list—you should utilize a foreach loop rather than relying solely on numeric indexing:

foreach ($array as $item) {
    echo $item->id . ' - ' . $item->fname . '<br>';
}

This approach treats each $item as an object reference, allowing you to access nested properties cleanly. You do not need to worry about the internal structure of stdClass changing; it is a generic container provided by PHP specifically for this type of dynamic data retrieval.

Converting Objects to Associative Arrays

One common pitfall arises when you attempt to map this object-based structure into a traditional associative array format using array_map. If your downstream logic requires accessing properties via string keys (e.g., $data['id']), converting the objects might be necessary. PHP provides the (array) cast for this purpose:

$data = [];
foreach ($array as $obj) {
    $data[] = (array)$obj;
}

// Now you can access values like this:
echo $data[0]['id']; 

This conversion transforms the stdClass instance into a native array where properties become keys. While this offers flexibility, it introduces a trade-off: you lose type safety and specific object behaviors if your original code relied on method calls or specific object state. Additionally, converting every item in a large dataset can incur unnecessary overhead if you only need to read a few properties.

Debugging and Inspection

The print_r() output you provided clearly shows the structure, but when debugging complex data pipelines, inspecting objects directly in the code is more efficient than relying on print statements alone. If you are working within a Laravel application, leveraging tools like dd() or dump() allows you to see these structures without cluttering your logs:

var_dump($array[0]->id); 
// Outputs: int(25)

It is worth noting that stdClass objects do not have a specific constructor signature; they are populated dynamically. This means you cannot instantiate them with new stdClass(id: 25) syntax in older PHP versions, nor can you assume property types without checking. Always verify the existence of properties before accessing them in production code to prevent "Undefined property" warnings. You can use the isset() function or the null coalescing operator to handle missing data gracefully:

echo $array[0]->text ?? 'Default Text';

By understanding that $array['id'] treats the first element as a scalar key while $array[0]->id correctly identifies it as an object property, you can navigate these structures with confidence. This distinction is critical when migrating legacy data or integrating external APIs that return JSON converted into stdClass instances by default.

Our network