PHP Array stdClass Object - Echo Value

PHP Array stdClass Object - Echo Value

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

Stefan Bogdanescu

Founder & Senior Architect

Decoding the stdClass Object in PHP Arrays

When working with data returned from WordPress core functions or third-party libraries, you often encounter arrays where the elements are stdClass objects rather than simple associative arrays. This specific structure is common when retrieving taxonomy terms via wp_get_post_terms(). If your variable $arr holds an array of these objects, attempting to access a property directly with $arr['slug'] will result in an undefined index notice or return null. PHP treats the top-level keys as numeric indices (0, 1, 2...) when iterating through taxonomy terms, meaning you cannot skip straight to the slug key.

To retrieve the value "community-service" from your example data structure, you must first acknowledge that $arr is an indexed array containing objects. Each object within that array possesses its own set of properties, such as term_id, name, and taxonomy. The syntax for accessing a property on an object differs slightly from accessing a key in an associative array. You need to chain the indexing operation with the object property accessor.

The correct approach involves iterating through the array or using a direct index if you know the position of the term. Since $arr[0] represents the first stdClass object, you access its properties using the arrow operator (->). Therefore, the code to echo the slug value is:

echo $arr[0]->slug;

If your array might contain multiple terms or if the order is not guaranteed, iterating through the collection provides a more robust solution. You can use foreach to loop over each element and check if the specific term matches your criteria before echoing its slug. This prevents errors if the array contains empty slots or if you are looking for a specific category among several others.

foreach ($arr as $term) {
    echo $term->slug;
}

However, relying on numeric indices like $arr[0] can be brittle. In dynamic environments where data is fetched asynchronously or from different sources, the array might change shape. A safer pattern involves checking if the property exists before accessing it to avoid runtime warnings. You can combine this with a direct check to ensure you are dealing with an object and not a null value.

if (isset($arr[0]) && is_object($arr[0])) {
    echo $arr[0]->slug ?? 'default-slug';
}

Understanding the distinction between $arr['slug'] and $arr[0]->slug is crucial for debugging these types of issues. The former assumes $arr is an associative array where keys are strings like 'slug', while the latter acknowledges that $arr is an indexed list of objects. Many developers stumble here because they expect the data to be flat, but WordPress taxonomy queries return nested structures by default.

If you are building a custom plugin or theme and need to handle this data structure consistently, consider normalizing your data immediately after retrieval. Converting the stdClass objects into associative arrays using json_decode(json_encode($arr), true) creates a clean, flat array where you can safely use $arr[0]['slug']. While this adds a slight overhead in terms of memory and processing time, it simplifies subsequent logic and eliminates the need to remember object property accessors.

For projects built on Laravel, dealing with raw stdClass objects is less common since Eloquent models usually handle this abstraction automatically. However, if you are integrating legacy WordPress code into a modern stack, understanding these nuances remains essential. If you find yourself frequently converting between these data structures, leveraging tools or helper functions can streamline your workflow and reduce the cognitive load associated with switching between object and array syntax.

Our network