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?
asked Dec 26, 2016 at 11:40
Sid
1,2532 gold badges26 silver badges47 bronze badges
-
u can't explodeVijaya Vignesh Kumar– Vijaya Vignesh Kumar2016年12月26日 11:45:55 +00:00Commented Dec 26, 2016 at 11:45
-
u converting array to string so you must use implode functionVijaya Vignesh Kumar– Vijaya Vignesh Kumar2016年12月26日 11:46:20 +00:00Commented Dec 26, 2016 at 11:46
-
its json value???Vasim Shaikh– Vasim Shaikh2016年12月26日 11:47:51 +00:00Commented Dec 26, 2016 at 11:47
3 Answers 3
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
leoap
the correct symbol for key/value in foreach is
=>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.
answered Dec 26, 2016 at 11:49
Vasim Shaikh
4,5522 gold badges25 silver badges52 bronze badges
Comments
<?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>
answered Dec 26, 2016 at 11:56
user6533277
Comments
lang-php