簡體   English   中英

如何從 php 中的 JSON 對象數組中刪除值?

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

我有一個 PHP 代碼和 JSON,如下所示:

PHP代碼:

<?php if (!empty($_POST) && isset($_POST['savechanges']) && $_POST['savechanges'] == 1 && isset($_SESSION['pageadmin'])) {
      $output = array();     
      $output['en_desc']=$_POST['en_desc'];
      $output['code']=$_POST['code'];

      $fp = fopen('../feeds/ptp-ess_landing_scommittees.json', 'w');
      fwrite($fp, json_encode($output));
      fclose($fp);
   }
   if(file_exists('../feeds/ptp-ess_landing_scommittees.json')){
       $data = json_decode(file_get_contents('../feeds/ptp-ess_landing_scommittees.json'));
   }
?> 
<?php if($data) { ?>
    <form method="post" id="myform" style="text-align:left;">
      <input type="hidden" id="savechanges" name="savechanges" value="1">
       <div style="text-align:center; margin-right:9px; margin-bottom:24.703px;">
         <button type="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'; }?>

JSON :

{"code":["AEFA","AGFO"], "en_desc":["Foreign Affairs and International Trade","Agriculture and Forestry"]}

以下 DOM 是通過上面的 PHP/JSON 代碼生成的:

DOM(HTML):

<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="AEFA">
  <input type="text" name="en_desc[]" value="Foreign Affairs and International Trade">
</div>

<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="AGFO">
  <input type="text" name="en_desc[]" value="Agriculture and Forestry">
</div>

以下 JS 代碼在單擊刪除按鈕時從 DOM 中刪除一行。 刷新頁面時,刪除的行再次返回,因為所有內容都通過 JSON 呈現。

JS代碼:

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

問題陳述:

上面的 JS 代碼正在從 DOM 中刪除行(單擊刪除按鈕),但在刷新頁面時,所有內容都會再次呈現。

我想知道我需要添加什么 PHP 代碼,以便在通過 JS 從 DOM 中刪除行時,它會在保存表單時從 JSON 中刪除值。

第 1 步:用戶單擊刪除按鈕從 DOM 中刪除該行。
第 2 步:在保存表單並呈現頁面時,不應存在已刪除的行。

我知道我必須使用 unset 函數才能從 JSON 中刪除值,但我不確定如何將它集成到表單中。

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'));

看看“s” :)

編輯:您還保存了新文件,而沒有實際檢查是否有帖子發生,這是完整的代碼:

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM