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.

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

报告相同问题?

悬赏问题

  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题