0

I have a $content variable, echo $content shows the following :

{
   "ID": 181271,
   "version_id": 2137,
   "theme_id": 2,
   "score": null,
   "showstopper": 0
}

Obviously echo $content['ID'] should shows 181271

So I need to iterate all fields. I'm doing:

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

And it gives me some crazy result!

incrementing 1 exists 1 wasRecentlyCreated timestamps 1

Expected result:

ID 181271
version_id 2137
etc...
John P
  • 15,035
  • 4
  • 48
  • 56
user453575457
  • 464
  • 6
  • 21

2 Answers2

2

Hope this will work, PHP code demo

Convert json string to array with this $array = json_decode($content, true);

$content = '{"ID":181271,"version_id":2137,"theme_id":2, "score":null,"showstopper":0}';
$array = json_decode($content, true);
foreach ($array as $key => $value)
{
    echo $key;
    echo $value;
}
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
1

For it is a json string, not an array. You have to json_decode in into an array. Then you can access it.

LF00
  • 27,015
  • 29
  • 156
  • 295