我可能在PHP代码中遗漏了一些东西。我想如果data.json文件不包含任何带有发布ID的数组,那么创建它是否在那里然后将该点更改为+1。对不起,如果我不太清楚我的英语不是最好的。 这是我的代码感谢您的帮助。
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
$date = date("c");
$id = $_POST['id'];
$file = 'data.json';
$nom = $_POST['nom'];
$description = $_POST['description'];
$adresse = $_POST['adresse'];
$telephone = $_POST['telephone'];
$img = $_POST['img'];
$points = $_POST['points'];
$data1 = array(
"date" => $date,
"ID" => $id,
"nom" => $nom,
"description" => $description,
"adresse" => $adresse,
"telephone" => $telephone,
"img" => img,
"points" => "1",
);
$replace=array( /* Replace these keys with these values */
'date' => $date,
'points' => ++$points,
);
$keys=array_keys( $replace );
$strjson= file_get_contents($file);
$json=json_decode( $strjson );
for ($i = 0; $i < count($json); $i++) {
// if we found it,
if ($json[$i]->id != $id) {
array_push($json, $data1);
$jsonData = json_encode($json);
file_put_contents($file, $jsonData);
} else {
foreach( $replace as $key => $value ){
$obj->$key=$value;
}
$json = array_values($json);
$fp = fopen($file, 'w');
fwrite($fp, json_encode($json));
fclose($fp);
}
}
?>
答案 0 :(得分:1)
"img" => img
应该是
"img" => $img
请将以下内容视为未经过测试,除非此项成为已接受的答案或OP对此答案发表评论。如果您发现任何问题,请随时编辑或发表评论。 :)
我认为实现你的意思的最好方法是首先从解码的JSON文件中获取一个数组。所以我们用
实现了这个目标 $json = json_decode($strjson, true);
确实根据documentation,第二个论点。
当为TRUE时,返回的对象将被转换为关联数组。
当您搜索ID并且我们现在将JSON转换为关联数组格式时,我们只需搜索数组是否具有给定键。我们用
来实现这一目标// As you want to push ID if not found, I consider that I should search for ID
array_key_exists('ID', $json)
根据documentation,第一个参数是我们搜索的关键,第二个参数是我们必须搜索的数组。
array_key_exists()如果在数组中设置了给定的键,则返回 TRUE 。 键可以是数组索引的任何可能值。
除了我不明白你使用$obj->$key=$value;
作为$obj
的事实,我认为在你的原始文件中,你让事情变得有点复杂......
现在我们有一个数组格式的$ json,实现替换/更新的最简单方法是使用循环从$replace
读取键和值,并将数据替换为具有具有根据$replace[$key]
换句话说,做以下事情会更好
// If we find the ID and the ID is different from the current $id, we update
// some values
foreach ($replace as $key => $value) {
// We get the $replace[$key] and update $json[$key] with the value of $replace[$key]
$json[$key] = $value;
}
大于
foreach( $replace as $key => $value ){
$obj->$key=$value;
}
根据之前解释的所需更改,这是最终文件
<?php
// We set the header
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: X-Requested-With');
// We assign into a variable the file name
$file = 'data.json';
// We assign/get the data we gonna save into the file
$date = date('c');
$id = $_POST['id'];
$nom = $_POST['nom'];
$description = $_POST['description'];
$adresse = $_POST['adresse'];
$telephone = $_POST['telephone'];
$img = $_POST['img'];
$points = $_POST['points'];
// We create an array with the needed information to encode into JSON format
$data1 = array(
'date' => $date,
'ID' => $id,
'nom' => $nom,
'description' => $description,
'adresse' => $adresse,
'telephone' => $telephone,
'img' => $img,
'points' => '1',
);
// We manage here the data that need to be updated in case we do not match 'ID '
// or 'ID' from JSON file is different from the current $id
$replace = array(
'date' => $date,
'points' => ++$points,
);
// We get the content of the JSON file
$strjson = file_get_contents($file);
// We decode it into an associative array
$json = json_decode($strjson, true);
// We preset the data (in certain way to apply DRY... More info there : https://en.wikipedia.org/wiki/Don%27t_repeat_yourself))
$jsonData = json_encode(array());
if (array_key_exists('ID', $json) && $json['ID'] != $id) {
// If we find the ID and the ID is different from the current $id, we update
// some values
foreach ($replace as $key => $value) {
// We get the $replace[$key] and update $json[$key] with the value of $replace[$key]
$json[$key] = $value;
}
// We encode into JSON format
$jsonData = json_encode($json);
} else {
// We save the new data into the file
// I do prefer:
// $json = array_merge($json, $data1);
// but the following should work (not tested)
// Also, there's no need to push ...
// array_push($json, $data1); // ==> Commented because this is not needed if I understood correctly what you're trying to do
$jsonData = json_encode($data1);
}
// To apply DRY
// We save the data into the file
file_put_contents($file, $jsonData);
?>
array_push($json, $data1);
?我对此进行了评论,因为在我看来,我们将$data1
的内容推送到文件中,因此无需使用$json
变量......