I am trying to loop through some json files, and combine them into one json file. My plan is to have a global $allData
array, and just merge new candidates into them.
我试图循环一些json文件,并将它们组合成一个json文件。我的计划是拥有一个全局$ allData数组,并将新候选者合并到它们中。
<?php
$allData = array();
$count = 0;
if ($handle = opendir('./json/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo $entry."<br />";
$source_file = file_get_contents('./json/'.$entry);
$data = json_decode($source_file,TRUE);
if($data != null){
$allData = array_merge($allData,$data);
echo "<br /><br /><br /><br /> !!!! <br /><br /><br /><br />";
print_r($allData);
}
else{
echo "Invalid Json File";
} //end else
}
closedir($handle);
}
echo "<br /><br /><br /><br /> !!!! <br /><br /><br /><br />";
print_r($allData);
However, the merge is overwriting the file. How can I combine multiple json files into one?
但是,合并将覆盖该文件。如何将多个json文件合并为一个?
I would like the following result:
我想得到以下结果:
1.json:
{"date":"10"},{"comment":"some comment"},{"user":"john"}
2.json:
{"date":"11"},{"comment":"another quote"},{"comment":"jim"}
combined.json
[{"date":"10"},{"comment":"some comment"},{"user":"john"},
{"date":"11"},{"comment":"another quote"},{"comment":"jim"}]
I am only getting one of these values after I merge the arrays.
合并数组后,我只获得其中一个值。
[{"date":"25.4.2013 10:40:10"},{"comment":"some text"},{"comment":"some more text"},
[{"date":"25.4.2013 10:45:15"},{"comment":"another quote"},{"comment":"quote"}]]
2 个解决方案
#1
8
Your merge is odd:
你的合并是奇怪的:
$result = array_merge($allData,$data);
You want to be merging each new $data
array onto a growing $allData
array right? I think you want to do this instead:
你想将每个新的$ data数组合并到一个不断增长的$ allData数组上吗?我想你想这样做:
$allData = array_merge($allData,$data);
Also you can get rid of this, it's not necessary.
你也可以摆脱这个,这是没有必要的。
if($count == 0){
$allData = $data;
}
#2
2
As stated before, $result = array_merge($allData,$data);
is the best way to merge arrays. This code works to merge the two, however, the problem was that my json was not properly formed.
如前所述,$ result = array_merge($ allData,$ data);是合并数组的最佳方法。这段代码可以合并两者,但问题是我的json没有正确形成。
I didn't have []
around my two files, so they could not be merged properly.
我的两个文件周围没有[],因此无法正确合并。
#1
8
Your merge is odd:
你的合并是奇怪的:
$result = array_merge($allData,$data);
You want to be merging each new $data
array onto a growing $allData
array right? I think you want to do this instead:
你想将每个新的$ data数组合并到一个不断增长的$ allData数组上吗?我想你想这样做:
$allData = array_merge($allData,$data);
Also you can get rid of this, it's not necessary.
你也可以摆脱这个,这是没有必要的。
if($count == 0){
$allData = $data;
}
#2
2
As stated before, $result = array_merge($allData,$data);
is the best way to merge arrays. This code works to merge the two, however, the problem was that my json was not properly formed.
如前所述,$ result = array_merge($ allData,$ data);是合并数组的最佳方法。这段代码可以合并两者,但问题是我的json没有正确形成。
I didn't have []
around my two files, so they could not be merged properly.
我的两个文件周围没有[],因此无法正确合并。