일부 json 파일을 반복하여 하나의 json 파일로 결합하려고합니다. 내 계획은 세계 $allData
배열을 가지고 있으며, 새 후보를 병합하기 만하면됩니다.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"}]]
json_decode() 데이터와 함께 array_merge()를 사용해 보셨습니까? – PKeidel