0

I have a json string derived from MySQL query using GROUP_CONCAT (which returns STRING).

Here's JSON string as field portfolios:

[
  {name:"Branch GBD_1.pdf", img_path:"1333752771.pdf"},
  {name:"Doc.pdf", img_path:"2020107119.pdf"},
  {name:"COK.pdf", img_path:"264860069.pdf"}
]

Now in php, I tried to decode the field and loop through it, but I am not being able to.

foreach($records as $r)
{
    $varArray = json_decode($r['portfolios'], true);
    foreach($varArray as $que)
    {
        echo $que['name'].' '.$que['img_path'];
        echo '<br/>';
    }           
}

How do I break or convert the variable into loop-able object?

Hasan Khan
  • 633
  • 1
  • 8
  • 15
Azima
  • 3,835
  • 15
  • 49
  • 95

1 Answers1

3

$varArray is not an array because your json string is not valid json. The keys need to be wrapped in doublequotes.

[
  {"name":"Branch GBD_1.pdf", "img_path":"1333752771.pdf"},
  {"name":"Doc.pdf", "img_path":"2020107119.pdf"},
  {"name":"COK.pdf", "img_path":"264860069.pdf"}
]
Hasan Khan
  • 633
  • 1
  • 8
  • 15
James
  • 1,138
  • 9
  • 13