How to extract value by key from json response in PHP

How to extract value by key from json response in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

How to extract value by key from json response in PHP

When parsing API responses, it is easy to fall into the trap of assuming every return object will be an associative array. In the example you provided, the var_dump output reveals a stdClass object containing a single property named updated. While this structure maps directly to your needs conceptually, accessing that data requires understanding PHP's type system and how it handles dynamic properties versus static arrays.

The core issue is that $result is not an array; it is an instance of stdClass. To access the value associated with the key "update", you must treat the object as such. However, there is a critical discrepancy in your data: the object property is named updated, but you are requesting to extract the key update. If you attempt to access $result->update, PHP will return null because that specific property does not exist on the object instance.

To retrieve the value 1, you must match the actual property name defined in the response. Since the stdClass allows for dynamic properties, you can access this directly using the arrow operator:

$value = $result->updated;
// $value will be 1

If your application logic specifically requires the data to live under an index named "update" (perhaps to interface with a codebase expecting that specific string key), you have two primary options. The first is to alias the property immediately upon retrieval. You can assign the existing value to a new variable or array index:

$targetValue = $result->updated;
$result['update'] = $targetValue;

Now, $result contains both the original object structure and the new key you requested. This approach preserves the original data while providing a convenient handle for your specific use case.

Alternatively, if you need to work with the response as an associative array from the start, converting the stdClass object is a common pattern. You can use (array) casting to transform the object into an array representation:

$arrayResult = (array) $result;
// Accessing via the actual property name
$actualValue = $arrayResult['updated']; 
// Note: This will still be null if accessed as 'update' without aliasing

Be cautious, however, regarding dynamic properties created from JSON. If the incoming JSON string is decoded using json_decode() without the second argument set to true, PHP defaults to returning a stdClass object rather than an array. While this works for simple access, it complicates iteration and merging operations. Explicitly decoding with json_decode($jsonString, true) ensures you always receive an array, which can prevent accidental null returns when property names don't match expected keys exactly.

In your specific scenario, the mismatch between the JSON key updated and your requested key update is the root cause of any potential failure. Ensure your downstream logic accounts for this discrepancy. If you are working within a Laravel application, accessing Eloquent models or collections might handle these transformations differently, but for raw API responses, direct property access remains the most efficient solution.

For more complex scenarios involving nested objects or strict schema validation, consider using packages that provide type hints and helper methods to safely extract data without relying on loose dynamic properties. The laravelcompany.com resource often covers advanced patterns for handling JSON payloads in modern stacks, which can be useful if you move beyond simple property access into structured data transformation pipelines.

Ultimately, always validate the structure of your response before attempting extraction. Relying on dynamic properties is flexible but fragile; knowing exactly what the API returns allows you to choose between direct access, aliasing, or full decoding based on the robustness required by your application.

Our network