【问题标题】:I can't generate the correct Json file in PHP我无法在 PHP 中生成正确的 Json 文件
【发布时间】:2021-08-05 03:51:01
【问题描述】:

它不能正常工作 请帮我获取正确的 json 文件?

特别是当我在 php 中将它插入网站时,我得到了这个数组,因此 Json 文件可以正确创建它

这是我通过提交表单生成的第一个数组。 我将此数组添加到 Json 文件的末尾,但得到了不需要的结果。

// CODE ---------------------------
  $_arr_POST    = array();
  $_arr_POST    = $_POST; // POST from FORM
  foreach ($_arr_POST as $key => $element) {
    $arr_index_POST[$key] = $element;
  }
  $data_path        = "path/file.json";
  $_GET_JSON        = file_get_contents($data_path);
  $_array_GET_Json  = json_decode($_GET_JSON,true);

if (count($_array_GET_Json) != 0 ){
    foreach ($_array_GET_Json as $key =>  $element) {
      $_array_element_GET = $element;
     }
     $_json_PUT[]   = array_merge ($_array_index_GET , $arr_index_POST);
  } else {
     $_json_PUT[]   = $arr_index_POST;
  }

file_put_contents($data_path, json_encode($_json_PUT));

得到这样一个格式化的数组,那可不行!

Array
(
    [0] => Array
        (
                [id] => 1
                [datetime] => 2021-08-05 05:11:34
                [product] => farfalle pink
                [code] => ff01pink
                [company_name] => Rome Capitale
                [status] => active
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 2
                                    [datetime] => 2021-08-05 05:11:34
                                    [product] => farfalle pink
                                    [code] => ff01pink
                                    [company_name] => Rome Capitale
                                    [status] => active
                                )

                            [id] => 3
                            [datetime] => 2021-08-05 05:11:34
                            [product] => farfalle pink
                            [code] => ff01pink
                            [company_name] => Rome Capitale
                            [status] => active
                        )
                [id] => 0
                [datetime] => 2021-08-05 05:11:34
                [product] => farfalle pink
                [code] => ff01pink
                [company_name] => Rome Capitale
                [status] => active
                )

        )

)

这是我必须得到的结果 一个简单的 json 文件,如下所示


[
  {
    "id": "0",
    "datetime": "2021-08-03 05:00:34",
    "product": "farfalle pink",
    "code": "ff01pink",
    "company_name": "Rome Capitale",
    "status": "active"
  },
  {
    "id": "1",
    "datetime": "2021-08-04 05:11:34",
    "product": "farfalle blue",
    "code": "ff01blue",
    "company_name": "Rome Capitale",
    "status": "active"
  },
  {
    "id": "100",
    "datetime": "2021-08-05 05:11:34",
    "product": "farfalle white",
    "code": "ff01white",
    "company_name": "Rome Capitale",
    "status": "active"
  }
]

【问题讨论】:

    标签: php arrays json array-merge


    【解决方案1】:

    我不知道你为什么在你的代码中过于复杂化。 如果您只想附加发布的数据,您可以使用以下 sn-p

    <?php
        $file = "path/file.json";
    
        if (!empty($_POST)) {
            $data = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
            if (!$data) $data = []; // Test if json_decode returned false or null (due to an invalid json string)
            $data[] = $_POST;
            file_put_contents($file, json_encode($data));
        }
    

    我强烈建议您在保存/附加到 JSON 文件之前先验证提交的数据

    【讨论】:

    • 谢谢,这是一个很棒的提示!
    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 2012-11-02
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    相关资源
    最近更新 更多