2016-06-16 42 views
-2

我想将所有json文件合并为一个。但是,我总是收到一个空的json文件。这是代码;将许多json文件合并为一个

function mergejson() 
{ 
    $events = array(); 
    // open each jsonfile in this directory 
    foreach(glob("*.json") as $filename) { 
     // get the contents of the the current file 
     $data[] =json_decode($filename, true); 
     $events= array_merge($events,$data); 
    } 
    $file_name ='merge.json'; 
    $events =json_encode($events,true); 
    file_put_contents($file_name,$events);  
} 

回答

3

函数json_decode将字符串作为第一个参数,而不是文件名!

所以你要加载的文件内容,请尝试使用file_get_contents

+0

请原谅我的知识的缺乏,我是初学者,你可以建议任何代码作为一个解决方案。 – Gvep

+0

只需用'json_decode(file_get_contents($ filename),true)替换'json_decode($ filename,true);'并且它应该可以工作! – cb0

+0

表示运行良好,谢谢! – Gvep