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?

1 Answer 1

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"}
]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot... that worked. some resources confused me with the quotes..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.