我正在使用一个简单的 PHP 脚本,它将简单 HTML 表单中填写的数据解析为 JSON 文件。问题是我无法使所有添加的对象出现在同一个数组中,如下所示:
{
"arraynamefoo": [
{
"name": "Testing¹",
"latitude": "32.75039432",
"longitude": "-117.01482831",
"description": "Testing, first description...",
"category": "park",
"photourl": "http://www.availableideas.com/wp-content/uploads/2016/10/Colorful-Polygonal-Render-iPhone-6-Plus-HD-Wallpaper.jpg"
},
{
"name": "Testing²",
"latitude": "32.7896552",
"longitude": "-117.1686369",
"description": "Testing, second description. . .",
"category": "park",
"photourl": "http://1.bp.blogspot.com/-olGHQWTmk3A/T0tZVSU3CDI/AAAAAAAABbA/EgjurSou6_Q/s1600/12.jpg"
},
{
"name": "Testing³",
"latitude": "32.89119111",
"longitude": "-116.86513959",
"description": "Testing, third description.",
"category": "park",
"photourl": "http://getwallpapers.com/wallpaper/full/c/f/0/674534.jpg"
}
]
}
我什至无法在 PHP 脚本中设置给定的数组名称,所以我也无法使所有写入的数据都属于同一个 JSON 数组......所以,有人可以帮我指出我的意思需要修改 PHP 代码才能实现这一点吗?请提前致谢。
write.php
文件:
<?php
$filetxt = 'file.json';
if(isset($_POST['name']) && isset($_POST['latitude']) && isset($_POST['longitude']) && isset($_POST['description']) && isset($_POST['category']) && isset($_POST['photourl'])) {
if(empty($_POST['name']) || empty($_POST['latitude']) || empty($_POST['longitude']) || empty($_POST['description']) || empty($_POST['category']) || empty($_POST['photourl'])) {
echo 'You need to fill all the fields';
}
else {
$data = array(
'name'=> $_POST['name'],
'latitude'=> $_POST['latitude'],
'longitude'=> $_POST['longitude'],
'description'=> $_POST['description'],
'category'=> $_POST['category'],
'photourl'=> $_POST['photourl'],
);
$filetxt = 'file.json';
$arr_data = array();
if(file_exists($filetxt)) {
$jsondata = file_get_contents($filetxt);
$arr_data = json_decode($jsondata, true);
}
$arr_data[] = $data;
$jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);
if(file_put_contents('file.json', $jsondata)) echo 'Successfully sent data!';
else echo 'Error while attempting to write to JSON';
}
}
else echo 'Error writing data to JSON!';
?>
另外,
write.html
文件(也许这个问题不需要它,但无论如何它都在这里):
<form action="write.php" method="post">
Name: <input type="text" name="name" id="name" /></br>
Latitude: <input type="text" name="latitude" id="latitude" /></br>
Longitude: <input type="text" name="longitude" id="longitude" /></br>
Description: <input type="text" name="description" id="description" /></br>
Category: <input type="text" name="category" id="category" /></br>
Photo URL: <input type="text" name="photourl" id="photourl" /></br>
<input type="submit" id="submit" value="Submit" />
我在下面重写了您的代码,并添加了一些注释来解释我在做什么。
<?php
// Set constants as such
const FILEPATH = 'file.json';
const FIELDS = [
'name',
'latitude',
'longitude',
'description',
'category',
'photourl'
];
// Loop over the fields you require rather than typing them out (so much less work!)
foreach (FIELDS as $fieldName) {
if (!array_key_exists($fieldName, $_POST) || strlen($_POST[$fieldName]) <= 0) {
echo 'You need to fill all the fields.';
die();
}
}
$arr_data = [
'arraynamefoo' => []
];
if (file_exists(FILEPATH)) {
$jsonString = file_get_contents(FILEPATH);
$arr_data = json_decode($jsonString, true);
}
// arraynamefoo is the name you showed in your pastebin.
// Adding the trailing [] will append the contents of $_POST to 'arraynamefoo'
$arr_data['arraynamefoo'][] = $_POST;
$jsonString = json_encode($arr_data, JSON_PRETTY_PRINT);
if (file_put_contents('file.json', $jsonString)) {
echo 'Successfully sent data!';
die();
}
echo 'Error while attempting to write to JSON';