【发布时间】:2018-06-14 10:00:54
【问题描述】:
我正在尝试将文件中可用的不同 json 的所有键添加到数组中。我目前所做的是:
//Get the json file content
$jsonData = file(__DIR__ .'/../logs/error.json');
//Save all the json
$json = [];
//Iterate through the line of the file, each line is a json
foreach($jsonData as $line)
{
//Convert the json in an associative array
$array = json_decode($line, true);
//Iterate through the json keys
foreach($array as $k => $val)
{
$json[$k] = $val;
}
}
json文件是这样的:
{"Timestamp":"2018-06-14T10:46:52.3326036+02:00","Level":"Error","MessageTemplate":"System.Exception"}
{"Timestamp":"2018-06-14T10:47:22.7493871+02:00","Level":"Error","MessageTemplate":"System.Exception"}
我会得到这个:
{"Timestamp":"2018-06-14T10:47:22.7493871+02:00","Level":"Error","MessageTemplate":"System.Exception"}
因为$json[$k]覆盖了我猜是之前的数组,但是$k是新的json那么为什么要替换数组的索引呢?
提前感谢您的帮助。
【问题讨论】: