How to loop through JSON object values in PHP?

How to loop through JSON object values in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

The Trap in Your JSON Loop

The code snippet you are looking at contains a fundamental misunderstanding of how PHP handles decoded JSON data and how loops actually iterate over arrays. When you run json_decode($json, TRUE), the resulting $obj is an associative array where the keys are strings like "1", "2", and so on, while the values are the actual content: 'a', 'b', etc.

The specific error in your example stems from two distinct issues inside the for loop condition. First, you are trying to access $obj['a']. Because your keys are numeric strings ("1", "2"), there is no key literally named 'a'. This will result in an empty array or a non-existent index error depending on your PHP version and error reporting settings. Second, the count() function returns the number of elements in that specific subset. If you attempt to loop based on a count of zero (because the key doesn't exist), your loop body never executes.

To display the values 'a', 'b', 'c', 'd', and 'e', you need to iterate over the array itself, not a non-existent subset of it. In modern PHP development, the most robust way to handle this is by leveraging the foreach construct. This approach abstracts away the internal indices entirely, allowing you to access only what matters: the key-value pairs.

Here is the corrected implementation using foreach:

$json = '{"1":"a","2":"b","3":"c","4":"d","5":"e"}';
// Note: The source values were single quotes (a), which in JSON must be quoted strings or true booleans/numbers.
// Assuming standard string values for a realistic example.
$obj = json_decode($json, TRUE);

foreach ($obj as $key => $value) {
    echo "$value";
}

In this version, we iterate directly over $obj. The variable $key holds the original string index (like "1"), and $value holds the content (like "a"). This ensures that even if your JSON keys are unordered or complex, you will capture every single value in the dataset.

If your goal is strictly to output the values without caring about their original keys, you can simply iterate over the array values:

foreach ($obj as $value) {
    echo $value;
}

This pattern is significantly safer than using a standard for loop with a numeric counter, which requires you to manually manage indices. If your JSON object were sparse (e.g., missing keys "2" and "4"), a for loop relying on count() or sequential index checking would skip the values at those positions entirely. A foreach loop guarantees that every element present in the decoded array is processed exactly once.

There are scenarios where you might need to preserve the order of insertion, which PHP 7.3+ and later versions handle naturally when decoding JSON objects into associative arrays. However, if you need to manipulate these values further or pass them to another function, keeping the key-value pair intact during iteration is almost always the right choice.

For developers working within the Laravel ecosystem, handling this data structure often involves passing the decoded array directly to controllers or views. If you are integrating this logic into a larger application using the framework's collection helpers, converting the raw array to a Collect instance provides additional utility methods for mapping and filtering before you ever need to loop manually.

Ultimately, the decision to use foreach over a for loop in this context is about reliability. You are trading the explicit control of a numeric counter for the implicit safety of iterating over the actual data structure provided by json_decode. This prevents off-by-one errors and ensures you never accidentally access a key that doesn't exist.

Our network