Php 如何防止数组中的重写?
我试图将文件中可用的不同json的所有键添加到数组中。目前我所做的是:Php 如何防止数组中的重写?,php,json,Php,Json,我试图将文件中可用的不同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)
//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
那么为什么要替换数组的索引呢
提前感谢您的帮助。可能这是您的预期输出
//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);
$temp = [];
//Iterate through the json keys
foreach($array as $k => $val)
{
$temp[$k] = $val;
}
$json[] = $temp;
}
也许这是你期望的结果
//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);
$temp = [];
//Iterate through the json keys
foreach($array as $k => $val)
{
$temp[$k] = $val;
}
$json[] = $temp;
}
换行
foreach($array as $k => $val)
{
$json[$k] = $val;
}
到
换行
foreach($array as $k => $val)
{
$json[$k] = $val;
}
到
您正在覆盖具有相同名称的键,因此在您的输出中没有什么令人惊讶的 您可能打算这样做:
foreach($jsonData as $line) {
$tmp = []; //<-- set up a new array just for this iteration
$array = json_decode($line, true);
foreach($array as $k => $val) $tmp[$k] = $val;
$json[] = $tmp; //<-- log the array in the master $json array
}
foreach($jsonData作为$line){
$tmp=[];//$val)$tmp[$k]=$val;
$json[]=$tmp;//您正在用相同的名称覆盖键,因此您的输出没有什么令人惊讶的
您可能打算这样做:
foreach($jsonData as $line) {
$tmp = []; //<-- set up a new array just for this iteration
$array = json_decode($line, true);
foreach($array as $k => $val) $tmp[$k] = $val;
$json[] = $tmp; //<-- log the array in the master $json array
}
foreach($jsonData作为$line){
$tmp=[];//$val)$tmp[$k]=$val;
$json[]=$tmp;////获取json文件内容
$jsonData=file(DIR'/../logs/error.json')
//在关联数组中转换json
$array=json_decode($jsonData,true)
//保存所有json
$json=[]
//迭代文件的每一行,每一行都是一个json
foreach($k=>$val的数组)
{
}//获取json文件内容
$jsonData=file(DIR'/../logs/error.json')
//在关联数组中转换json
$array=json_decode($jsonData,true)
//保存所有json
$json=[]
//迭代文件的每一行,每一行都是一个json
foreach($k=>$val的数组)
{
}