PHP写入JSON数据并追加
<?php
//输出
echo Write_log($_REQUEST['pid']);
/**
* 写入数据并追加
* @return string $array 需要写入的元素
*/
function Write_log($array){
$path = dirname(__FILE__).'/data.json';
if(!file_exists($path)){
fopen($path, "a");
}
$Getfile = file_get_contents($path);
$format = '['.rtrim($Getfile,",").']';
$data = json_decode($format,true);
$query = array_filter($data, function($vue) use ( $array){
if($vue['id'] == $array){return $vue['id'] == $array;}
});
if(!$query){
$Insert = array('id'=>$array);
$myfile = fopen($path, "a");
fwrite($myfile,json_encode($Insert).',');
fclose($myfile);
}
}