Javascript 如何在php中从JSON对象数组中删除值?

Javascript 如何在php中从JSON对象数组中删除值?,javascript,php,json,unset,Javascript,Php,Json,Unset,我有一个PHP代码和JSON,如下所示: PHP代码: 以下DOM是通过上面的PHP/JSON代码生成的: DOM HTML: 这里有一个输入错误: $data = json_decode(file_get_contents('../feeds/ptp-ess_landing_scommittees.json')); 应该是 $data = json_decode(file_get_contents('../feeds/ptp-ess_landing_committees.

我有一个PHP代码和JSON,如下所示:

PHP代码:

以下DOM是通过上面的PHP/JSON代码生成的:

DOM HTML:

这里有一个输入错误:

   $data = json_decode(file_get_contents('../feeds/ptp-ess_landing_scommittees.json'));
应该是

       $data = json_decode(file_get_contents('../feeds/ptp-ess_landing_committees.json'));
看看下面的例子:

编辑:您还保存了新文件,但没有实际检查是否有帖子发生,以下是完整代码:

<?php

if (isset($_POST['submit'])) {
  $output = array();
  $output['en_desc'] = $_POST['en_desc'];
  $output['code'] = $_POST['code'];

  $fp = fopen('../feeds/ptp-ess_landing_committees.json', 'w');
  fwrite($fp, json_encode($output));
  fclose($fp);
}

if (file_exists('../feeds/ptp-ess_landing_committees.json')) {
  $data = json_decode(file_get_contents('../feeds/ptp-ess_landing_committees.json'));
}

?>
<?php if ($data) { ?>
  <form method="post" id="myform" style="text-align:left;">
    <div style="text-align:center; margin-right:9px; margin-bottom:24.703px;">
      <button type="submit" name="submit">Save</button>
    </div>
    <?php foreach ($data->code as $key => $value) { ?>
      <div class="house-senate-committee" style="text-align:center; margin-top:15px;">
        <button type="button" onclick="removeRow(this)" style="margin-right:10px;">Delete</button>
        <input type="text" name="code[]" style="margin-right:10px;" value="<?= $data->code[$key] ?>">
        <input type="text" name="en_desc[]" value="<?= $data->en_desc[$key] ?>">
      </div>
    <?php } ?>
  </form>
<?php } else {
  echo 'Cannot read JSON settings file';
} ?>

<script>
  function removeRow(el) {
    el.parentNode.remove();
  }
</script>

有兴趣使用会话将数据与页面重新加载内存分离吗?我只是尝试了代码,实际上删除了,但没有恢复@不幸的是,我过去从未使用过SESSION。你收到我的问题了吗?刚刚发布了一个答案。我不知道你在哪里设置savechanges输入?你知道Javascript中的onclick事件吗?单击按钮,它会执行活动,但在刷新页面时,它会返回到原始状态。我知道onclick事件是什么。但是,您不想在单击save时更新json吗?您做了哪些更改?另外,我还没有粘贴完整的代码。这只是一个片段。您的.json文件中有一个输入错误,我还为按钮添加了一个名称,这样您就可以确保只在发布帖子时删除/更新json。
<script>
function removeRow(el) {
  el.parentNode.remove();
}
</script>
unset($data->code);
unset($data->en_desc);
   $data = json_decode(file_get_contents('../feeds/ptp-ess_landing_scommittees.json'));
       $data = json_decode(file_get_contents('../feeds/ptp-ess_landing_committees.json'));
<?php

if (isset($_POST['submit'])) {
  $output = array();
  $output['en_desc'] = $_POST['en_desc'];
  $output['code'] = $_POST['code'];

  $fp = fopen('../feeds/ptp-ess_landing_committees.json', 'w');
  fwrite($fp, json_encode($output));
  fclose($fp);
}

if (file_exists('../feeds/ptp-ess_landing_committees.json')) {
  $data = json_decode(file_get_contents('../feeds/ptp-ess_landing_committees.json'));
}

?>
<?php if ($data) { ?>
  <form method="post" id="myform" style="text-align:left;">
    <div style="text-align:center; margin-right:9px; margin-bottom:24.703px;">
      <button type="submit" name="submit">Save</button>
    </div>
    <?php foreach ($data->code as $key => $value) { ?>
      <div class="house-senate-committee" style="text-align:center; margin-top:15px;">
        <button type="button" onclick="removeRow(this)" style="margin-right:10px;">Delete</button>
        <input type="text" name="code[]" style="margin-right:10px;" value="<?= $data->code[$key] ?>">
        <input type="text" name="en_desc[]" value="<?= $data->en_desc[$key] ?>">
      </div>
    <?php } ?>
  </form>
<?php } else {
  echo 'Cannot read JSON settings file';
} ?>

<script>
  function removeRow(el) {
    el.parentNode.remove();
  }
</script>