drhzn3911 2017-09-15 21:01
浏览 72
已采纳

通过PHP更新JSON文件

I am trying to update a JSON file via php, but what the current code is doing is appending the new object instead of replacing it. Here's a snippet of the JSON:

[
    {
        "id": 3,
        "title": "SOME MODS",
        "date": "Aug\/Sept 2017",
        "done": "yes",
        "files changed": {
            "1": "index.html",
            "2": "style.css"
        },
        "backend changes\/additions": {
            "1": "added some stuff"
        },
        "additions": {
            "1": "logo.jpg"
        }
    }
]

Basically what I want to do is on click of a button call an AJAX function to hit a php file. Then check to see if the passed id matches an id from the JSON file, then updated the "done" property... if no make it yes, if yes make it no.

Here's my PHP code:

if ( isset( $_POST['comp'] ) ) {
   $newObj = $_POST['comp'];

   $jsonFile = file_get_contents('data.json');
   $temp = json_decode($jsonFile, true);

   $tempArray;

   foreach( $temp as $e ){
      if( $e['id'] == $newObj ) {
         if ( $e['done'] === 'no' ){
            $e['done'] = 'yes';
            $tempArray = $e;
         } else {
            $e['done'] = 'no';
            $tempArray = $e;
         }
      }
   }

   $temp[] = $tempArray;
   $final_data = json_encode($temp, JSON_PRETTY_PRINT);
   file_put_contents('data.json', $final_data);

   print_r( $temp );

But as I mentioned, as it stands all it is doing is inserting a new Object instead of replacing. What do I need to change to make this functionality work?

Thank you all.

-S

  • 写回答

1条回答 默认 最新

  • duandu2980 2017-09-15 21:12
    关注

    You want to pass $e by reference in your foreach:

    <?php
    // this originally comes from file
    $json = <<<EOT
       [
        {
            "id": 3,
            "title": "SOME MODS",
            "date": "Aug\/Sept 2017",
            "done": "yes",
            "files changed": {
                "1": "index.html",
                "2": "style.css"
            },
            "backend changes\/additions": {
                "1": "added some stuff"
            },
            "additions": {
                "1": "logo.jpg"
            }
        }
    ]
    EOT;
    
    $temp = json_decode($json, true);
    $newObj=3;  // this is only here for testing
    
             // here's the trick: the & before $e
    foreach( $temp as &$e ){
      if( $e['id'] == $newObj ) {
         if ( $e['done'] === 'no' ){
            $e['done'] = 'yes';
         } else {
            $e['done'] = 'no';
         }
      }
    }
    $final_data = json_encode($temp, JSON_PRETTY_PRINT);
    file_put_contents('data.json', $final_data);
    

    This way you don't need another array and can save $temp back to your json file.

    Short explanation (cracks, please correct me):
    in your foreach php makes a copy of $e, so you have to write it back - as you've tried.
    When you pass it by reference you work on the original item $e, not a copy. This way you can manipulate it directly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?