PHP如果有数据,请替换或写入数据
我的PHP代码中可能缺少一些东西。我希望如果data.json文件不包含任何带有发布ID的数组,那么创建它,如果它在那里,那么将点更改为+1。对不起,如果我不是很清楚,我的英语不是最好的。 这是我的代码,谢谢你的帮助PHP如果有数据,请替换或写入数据,php,json,Php,Json,我的PHP代码中可能缺少一些东西。我希望如果data.json文件不包含任何带有发布ID的数组,那么创建它,如果它在那里,那么将点更改为+1。对不起,如果我不是很清楚,我的英语不是最好的。 这是我的代码,谢谢你的帮助 <?php header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST'); header("Access-Control-Allow-Heade
<?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);
}
}
?>
打字错误
应该是
"img" => $img
回到你的案子上来
请考虑以下未经测试,除非这是一个公认的答案或OP对此评论作出评论。如果您发现任何问题,请随时编辑或留下评论:
将JSON转换为数组 我认为实现您的意思的最好方法是首先从解码的JSON文件中获取一个数组。因此,我们通过 $json = json_decode($strjson, true);
// As you want to push ID if not found, I consider that I should search for ID
array_key_exists('ID', $json)
事实上,根据第二个论点
如果为TRUE,则返回的对象将转换为关联数组
检查数组是否具有搜索的键
当您搜索ID时,我们现在将JSON转换为关联数组格式,我们只需搜索数组是否具有给定的键。我们通过以下方式实现这一目标:
$json = json_decode($strjson, true);
// As you want to push ID if not found, I consider that I should search for ID
array_key_exists('ID', $json)
第一个参数是我们搜索的键,第二个参数是我们必须搜索的数组
如果在数组中设置了给定的键,则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;
}
最终文件
根据前面解释的所需更改,这里是最终文件
<?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变量…data.json的内容是什么?对于$I=0,$list在下面是什么$我数$list++$我?我犯了个错误我会改正的。data.json只包含[]2个括号,大约$obj是多少?回答得很好,但让我印象深刻的是,如此薄弱的问题得到了如此彻底的回答。