PHP

How to iterate over a jSON using PHP2 min read

There are several ways to iterate over a JSON object in PHP. Here is one way using a foreach loop:

This will output:




You can also use a for loop to iterate over the object:

This will output:

You can also use array_keys and a foreach loop to get the keys and values separately:

This will output:

Here is an example of iterating over a JSON object in PHP:

This will output:

You can also use a for loop to iterate over the object:

This will also output:


The easiest way to iterate over jSON using php is to convert it to an array. It is simply a matter of using the json_decode function passing it as a parameter the jSON string that you want to go through and true (so that the result is returned as an associative array).

Once the jSON is converted to an array you can iterate over it as you would any other php array, using a foreach or some other structure.

For example, if you had the following text in the $json variable:

To loop through it from php you would do something like:

And the output you would get would be:

2 Comments

Leave a Comment