[英]How to read multiple values from JSON object array in php?
<?php
$output['toggle_multi_tiles']=$_POST['toggle_multi_tiles'];
$fp = fopen('../feeds/ptp-ess_landing.json', 'w');
fwrite($fp, json_encode($output));
fclose($fp);
logActivity();
if(file_exists('../feeds/ptp-ess_landing.json')){
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing.json'));
}
?>
I have a JSON as shown below: 我有一个JSON ,如下所示:
{"toggle_multi_tiles":["0","1","2","3"]}
What I want is from the php code below I want it to print as: 我想要的是来自下面的php代码,我希望将其打印为:
<p>Hello World<p>
<p>Logical World<p>
<p>Good Morning </p>
<p>Good Evening</p>
It should not print <p>Good Day</p>
它不应该打印
<p>Good Day</p>
Php code:
邮递区号:
<?php if ($data->{"toggle_multi_status"} == 1) {
if(in_array("0", $data->toggle_multi_tiles)) { ?>
<p>Hello World<p>
<?php } else if (in_array("1", $data->toggle_multi_tiles)) { ?>
<p>Logical World<p>
<?php } else if(in_array("2", $data->toggle_multi_tiles)) { ?>
<p> Good Morning </p>
<?php } else if(in_array("3", $data->toggle_multi_tiles)) { ?>
<p>Good Evening</p>
<?php }
<?php } else if(in_array("4", $data->toggle_multi_tiles)) { ?>
<p>Good Day</p>
<?php }
}
?>
$data->toggle_multi_tiles
is reading from JSON. $data->toggle_multi_tiles
正在从JSON读取。
Why not just build an output array with keys that match the numbers in the response that you want to check: 为什么不只使用与您要检查的响应中的数字匹配的键来构建输出数组:
$data = json_decode(file_get_contents('../feeds/ptp-ess_landing.json'));
$output = ['Hello World','Logical World','Good Morning','Good Evening','Good Day'];
foreach($data->toggle_multi_tiles as $value) {
echo isset($output[$value]) ? "<p>{$output[$value]}</p>" : "";
}
In this example $output
starts at 0
, to use different numbers: 在此示例中,
$output
从0
开始,以使用不同的数字:
$output = [2=>'Hello World',
4=>'Logical World',
6=>'Good Morning',
8=>'Good Evening','Good Day']; //etc...
If you want to do totally different things then you can use a switch
in the loop, either using the $output
or not : 如果您想做完全不同的事情,则可以在循环中使用一个
switch
,可以使用$output
或不使用$output
:
foreach($data->toggle_multi_tiles as $value) {
switch($value) {
case 0:
//complex HTML
break;
case 1:
//complex HTML
break;
case 2:
//complex HTML
break;
case 3:
//complex HTML
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.