无法将字符串与PHP中文件\u get\u contens检索到的另一个字符串连接起来

无法将字符串与PHP中文件\u get\u contens检索到的另一个字符串连接起来,php,json,string,concatenation,filereader,Php,Json,String,Concatenation,Filereader,所以我得到了一个打印以下日志的应用程序: {"date":"15/09/2016", "time":"09:29:58","temp":"17.0", "humidity":"95.0" }, {"date":"15/09/2016", "time":"09:30:01","temp":"17.0", "humidity":"95.0" }, {"date":"15/09/2016", "time":"09:30:03","temp":"17.0", "humidity":"95.0" },

所以我得到了一个打印以下日志的应用程序:

{"date":"15/09/2016", "time":"09:29:58","temp":"17.0", "humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:01","temp":"17.0", "humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:03","temp":"17.0", "humidity":"95.0" },
在PHP的帮助下,我阅读并打印如下,效果很好:

<?php   
 $logFile = file_get_contents( "../../../home/shares/flower_hum/humid.log" ); 
 echo $logFile;
?>

我可能要补充一点,我是在运行Raspberry Pi的Apache服务器上运行这个程序的,但是我已经安装了PHP,有些东西确实可以运行,所以我认为这与此无关。

您可以通过
trim
删除换行符来实现这一点,
rtrim
以除去
数组映射
回调中的尾随逗号。 还有一些来回的
json\u解码
json\u编码

见下文

<?php

$logLines = file('logfile.txt');

$entries = array_map("clean",$logLines);

$finalOutput = [
    'log'   => $entries
];

print json_encode($finalOutput, JSON_UNESCAPED_SLASHES);
// add the flag so the slashes in the dates won't be escaped

function clean($string){

    return json_decode(rtrim(trim($string),','),true);

}

为什么不直接使用
rtrim($logFile,,')
删除最后一个逗号呢?我尝试过,但似乎没有效果。到底是哪条语句引发了错误?当我将$logFile与括号连接起来时,这会导致错误。但我不知道是哪个错误,所有的chrome都在说HTTP 500,所以服务器错误可能意味着什么。这实际上是有效的!非常感谢!我已经解决这个问题一整天了。:)
{"log":[
{"date":"15/09/2016", "time":"09:29:58","temp":"17.0","humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:01","temp":"17.0", "humidity":"95.0" },
{"date":"15/09/2016", "time":"09:30:03","temp":"17.0", "humidity":"95.0" }
]}
<?php

$logLines = file('logfile.txt');

$entries = array_map("clean",$logLines);

$finalOutput = [
    'log'   => $entries
];

print json_encode($finalOutput, JSON_UNESCAPED_SLASHES);
// add the flag so the slashes in the dates won't be escaped

function clean($string){

    return json_decode(rtrim(trim($string),','),true);

}
{"log":[{"date":"15/09/2016","time":"09:29:58","temp":"17.0","humidity":"95.0"},{"date":"15/09/2016","time":"09:30:01","temp":"17.0","humidity":"95.0"},{"date":"15/09/2016","time":"09:30:03","temp":"17.0","humidity":"95.0"}]}