【问题标题】:Create and update JSON file using stored procedure使用存储过程创建和更新 JSON 文件
【发布时间】:2017-03-09 10:21:56
【问题描述】:

我想使用存储过程创建一个 JSON 文件,并在我的表数据发生更改时更新该 jsonfile。我不知道该怎么做。请问谁能帮我解决这个问题?

【问题讨论】:

    标签: php json codeigniter-3


    【解决方案1】:
    <?php
        $someNewData = array("foo"=>"bar"); //example
        if(!file_exists("./data.json")) {
          $file = fopen('data.json', 'w');
          fwrite($file, '');
          fclose($file);
        }
    
       $jsonData = json_decode(file_get_contents("./data.json"), true);
       if(!is_array($jsonData)){$jsonData=array();}
       if(isset($someNewData) && is_array($someNewData)){
          file_put_contents("./data.json", json_encode(array_merge($someNewData, $jsonData)));
       }
    

    希望对你有帮助:)

    【讨论】:

    • 您的代码是正确的。但是当我的表数据不是每次都更改时,我想更新那个 json 文件。
    猜你喜欢
    • 2022-07-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 2015-05-01
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多