2016-06-16 44 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