我想通過一些json文件循環,並將它們合併成一個json文件。我的計劃是建立一個全球性的數組,並將新的候選人合併到他們中。php將json數組合併成一個數組
<?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);
但是,合併覆蓋文件。我怎樣才能將多個json文件合併爲一個?
我想以下結果:
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"}]
我只獲得這些值之後我合併了數組。
[{"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"}]]
你試過用array_merge()和json_decode()編輯的數據嗎? – PKeidel