简体   繁体   English

如何从php中的JSON对象数组读取?

[英]How to read from JSON object array in php?

I have an html code as shown below. 我有一个html代码 ,如下所示。 The following html code displays list of checkboxes as shown in the screenshot below the code. 以下html代码显示复选框列表,如代码下方的屏幕快照所示。

 <?php      
        $output['toggle_multi_tiles']=$_POST['toggle_multi_tiles'];  

        $output['episode_status']=$_POST['episode_status'];

        $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'));
        }
 ?>

 <?php if($data){
 ?>
    <fieldset style="background-color:darkseagreen;">
       <input type="checkbox" id="ptp" value="0" name="toggle_multi_tiles[]" <?php if($data->{"toggle_multi_tiles[]"}==0){echo
          'checked';}?>>
       <label for="toggle-multi-off">PTP</label>
       <input type="checkbox" id="l'e" value="1" name="toggle_multi_tiles[]" <?php if($data->{"toggle_multi_tiles[]"}==1){echo
          'checked';}?>>
       <label for="position-one">L'E</label>
       <div>
          <button type="submit">Save</button>   //Line A
       </div>
    </fieldset>
<?php }  ?>                 

在此处输入图片说明

On hitting save button at Line A after selecting the first 2 check-boxes from the screenshot above , everything get save in JSON as shown below: 从上面的屏幕截图中选择前两个复选框后,在A行点击保存按钮,所有内容都将保存为JSON,如下所示:

{"toggle_multi_tiles":["0","1"]}


Problem Statement: 问题陈述:

The issue which I am having right now is after saving the 2 check-boxes, the only check-box which display on page refresh is the 1st one ( not both ). 我现在遇到的问题是保存2个复选框后,页面刷新上显示的唯一复选框是第一个( 不是两个 )。

($data->{"toggle_multi_tiles[]"}==0) from the html code is reading from JSON. html代码中的($data->{"toggle_multi_tiles[]"}==0)正在从JSON中读取。

($data->{"toggle_multi_tiles[]"}==1) from the html code is reading from JSON. html代码中的($data->{"toggle_multi_tiles[]"}==1)正在从JSON中读取。

在此处输入图片说明

The key in your object is toggle_multi_tiles , not toggle_multi_tiles[] , so $data->{"toggle_multi_tiles[]"} should be $data->toggle_multi_tiles . 对象中的键是toggle_multi_tiles ,而不是toggle_multi_tiles[] ,因此$data->{"toggle_multi_tiles[]"}应该是$data->toggle_multi_tiles

The value of this property is an array of strings, you can use in_array() to test whether a value is in it. 此属性的值是一个字符串数组,可以使用in_array()来测试其中是否包含值。

<input type="checkbox" id="ptp" value="0" name="toggle_multi_tiles[]" <?php if(in_array("0", $data->toggle_multi_tiles)){echo
  'checked';}?>>
<label for="toggle-multi-off">PTP</label>
<input type="checkbox" id="l'e" value="1" name="toggle_multi_tiles[]" <?php if(in_array("1", $data->toggle_multi_tiles)){echo
  'checked';}?>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM