How to loop through JSON array using PHP
You are probably having trouble because reviews is an array and you are trying to access it as a JSON object.
$obj = json_decode($data, TRUE);
for($i=0; $i<count($obj['reviews']); $i++) {
echo "Rating is " . $obj['reviews'][$i]["rating"] . " and the excerpt is " . $obj['reviews'][$i]["excerpt"] . "<BR>";
}
I'm not sure what exactly you want but I guess you want print it just for debugging right now. You can try with print_r($obj);
and var_dump($obj);
- they must print something, especially var_dump()
.
When you see the data, you can easily edit function a little bit, so you can do for instance print_r($obj->reviews)
or print_r($obj['reviews'])
, depending if $obj
is object or array.