0

I am using Caldera forms to store data on my WordPress site. When I save data for multiple choices (checkboxes) I get an array of data which is similar to following;

{"opt905217":"Option 1","opt2347462":"Option 2","opt905265":"Option 3","opt906845":"Option 4",}

How can I achiveve the following;

<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
<li>Option 4</li>
</ul>

I tried using explode. But the problem is I have to skip the option IDs (e.g. opt2347462)

How can I get a list of only the option values?

3
  • u can't explode Commented Dec 26, 2016 at 11:45
  • u converting array to string so you must use implode function Commented Dec 26, 2016 at 11:46
  • its json value??? Commented Dec 26, 2016 at 11:47

3 Answers 3

1
foreach ($array as $key => $value) {
    echo "<li>".$value."</li>"
}

And if your array is a json, first json_decode($array, true)

Sign up to request clarification or add additional context in comments.

1 Comment

the correct symbol for key/value in foreach is =>
0

As I see that you have JSON data.

$json = '{"opt905217":"Option 1","opt2347462":"Option 2","opt905265":"Option 3","opt906845":"Option 4",}';

$obj = json_decode($json);
print $obj->{'opt905217'}; // Option 1

Few Points to remember:

  • json_decode requires the string to be a valid json else it will return NULL.
  • In the event of a failure to decode, json_last_error() can be used to determine the exact nature of the error.
  • Make sure you pass in utf8 content, or json_decode may error out and just return a NULL value.

Comments

0
<?php
$abc = '{"opt905217":"Option 1","opt2347462":"Option 2","opt905265":"Option 3","opt906845":"Option 4"}';
$newarray = json_decode($abc,true); ?>
<ul>
<?php foreach($newarray as $key => $value){ ?>
    <li><?php echo $value; ?></li>
<?php }?>


</ul>

link

Comments

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.