0

I'm storing some values using the mysql json field type, in Sequel Pro I'm 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 straightforward

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

I'm not sure I'm doing things right.

2
  • First use json_decode then use in loop Commented Aug 30, 2016 at 12:39
  • What is the output of print_r($plan->features);? I don't see where you decode your json, nor where you assign it to the property features. Commented Aug 30, 2016 at 12:41

1 Answer 1

6

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

3 Comments

Store the json_string in a variable called $teststring and after that decode it as shown in my example and use it in foreach loop. Cool it will work..
@BigJobbies. Have you obtained the output or you face hindrance still. Let me know if my help is needed....
Welcome @BigJobbies. That is the reason i pointed out the Error at first for your knowledge. Glad to help you...

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.