我正在使用一个简单的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" />
答案 0 :(得分:0)
通常,StackOverflow喜欢更精确的问题。签出guide on how to ask good questions。我在下面重写了您的代码,并添加了一些注释以解释我在做什么。希望这能回答您的问题。
<?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';