There are several ways to iterate over a JSON object in PHP. Here is one way using a foreach
loop:
1 2 3 4 5 6 7 8 |
$json = '{"key1":"value1", "key2":"value2", "key3":"value3"}'; $obj = json_decode($json); foreach($obj as $key => $value) { echo $key . ": " . $value . "<br>"; } |
This will output:
1 2 3 4 5 |
key1: value1 key2: value2 key3: value3 |
You can also use a for
loop to iterate over the object:
1 2 3 4 5 6 7 8 9 10 |
$json = '{"key1":"value1", "key2":"value2", "key3":"value3"}'; $obj = json_decode($json); $array = (array) $obj; for ($i = 0; $i < count($array); $i++) { echo $array[$i] . "<br>"; } |
This will output:
1 2 3 4 5 |
value1 value2 value3 |
You can also use array_keys
and a foreach
loop to get the keys and values separately:
1 2 3 4 5 6 7 8 9 10 |
$json = '{"key1":"value1", "key2":"value2", "key3":"value3"}'; $obj = json_decode($json); $keys = array_keys((array)$obj); foreach($keys as $key) { echo $key . ": " . $obj->$key . "<br>"; } |
This will output:
1 2 3 4 5 |
key1: value1 key2: value2 key3: value3 |
Here is an example of iterating over a JSON object in PHP:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$json = '{"employees": [ { "firstName":"John", "lastName":"Doe" }, { "firstName":"Anna", "lastName":"Smith" }, { "firstName":"Peter", "lastName":"Jones" } ]}'; $obj = json_decode($json); foreach($obj->employees as $employee) { echo $employee->firstName . " " . $employee->lastName . "<br>"; } |
This will output:
1 2 3 4 5 |
John Doe Anna Smith Peter Jones |
You can also use a for
loop to iterate over the object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$json = '{"employees": [ { "firstName":"John", "lastName":"Doe" }, { "firstName":"Anna", "lastName":"Smith" }, { "firstName":"Peter", "lastName":"Jones" } ]}'; $obj = json_decode($json); for ($i = 0; $i < count($obj->employees); $i++) { $employee = $obj->employees[$i]; echo $employee->firstName . " " . $employee->lastName . "<br>"; } |
This will also output:
1 2 3 4 5 |
John Doe Anna Smith Peter Jones |
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:
1 2 3 4 5 6 7 8 9 10 11 |
{ "color1":"#f00", "color2":"#0f0", "color3":"#00f", "color4":"#0ff", "color5":"#f0f", "color6":"#ff0", "color7":"#000" } |
To loop through it from php you would do something like:
1 2 3 4 5 6 7 8 |
<?php $array = json_decode(file_get_contents( 'entry.json')); foreach($array as $name => $hex){ echo $name.'='.$hex ."<br>"; } |
And the output you would get would be:
1 2 3 4 5 6 7 8 9 |
color1=#f00 color2=#0f0 color3=#00f color4=#0ff color5=#f0f color6=#ff0 color7=#000 |
[…] here it makes me a bit nostalgic to talk about the older post I wrote for this blog, but I think knowing how to iterate over a JSON using PHP can contribute to […]
[…] Kaynak: https://www.code4example.com/php/how-to-iterate-over-a-json-using-php/ […]