BigJobbies BigJobbies - 3 months ago 10
JSON Question

PHP looping over json objects in my database

Im storing some values using the mysql json field type, in Sequel Pro im seeing them as follows

["feature1","feature2","feature3","feature4"]


In my PHP file, i can print out the contents using

echo $plan->features;


but what i want to do is look over each one so i can style them, but the problem is when i stick it in a foreach loop i get the following:

Invalid argument supplied for foreach()


My loop is straight forward

foreach ($plan->features as $features) {
}


Im not sure im doing things right.

Any help would be greatly appreciated.

Cheers

Answer

This is a JSON String

["feature1","feature2","feature3","feature4"]

User json_decode() for the above string

$string = json_decode($teststring,TRUE);

And after that you can for loop the variable or foreach the variable

foreach($string as $single_value)
{
     echo $single_value.'<br>';
}

Output:

feature1

feature2

feature3

feature4
Comments