Ekumahost Ekumahost - 2 months ago 9
JSON Question

How to loop through JSON object values in PHP?

I have a JSON Object and I want to loop through the values:

$json = '{"1":a,"2":b,"3":c,"4":d,"5":e}';
$obj = json_decode($json, TRUE);
for($i=0; $i<count($obj['a']); $i++) {
echo $i;
}


I want the
$i
to display the
abcde
that are the values in the object.

Answer

Try using.

$json = '{"1":"a","2":"b","3":"c","4":"d","5":"e"}';
 $obj = json_decode($json, TRUE);

foreach($obj as $key => $value) 
{
echo 'Your key is: '.$key.' and the value of the key is:'.$value;
}

p.s not tested ;-)