我无法在 PHP 中生成正确的 Json 文件
I can't generate the correct Json file in PHP
无法正常工作
请帮助我获取正确的 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
$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));
}
我强烈建议您在 saving/appending 到 JSON 文件之前先验证提交的数据
无法正常工作 请帮助我获取正确的 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
$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));
}
我强烈建议您在 saving/appending 到 JSON 文件之前先验证提交的数据