我有多个JSON文件(超过120个),我想使用array_merge_recursive
将其合并到一个文件中,但我收到Argument #1 is not an array
错误。
获取两个单独的文件可以获得所需的解决方案,但尝试遍历所有文件会给我带来错误。
对此的任何帮助都非常感激 - 我在下面的代码中尝试了很多很多不同的调整,没有什么能给我任何快乐。
//The folder where the files are kept
$filenames = glob('Week6/*.json');
//Loading one file to start the merge (it has some content),
//convert from JSON format to a usable array
$arrayThree = file_get_contents('newMerge.json');
$arrayThree = json_decode($arrayThree);
//Loop through all of the filenames…
foreach ($filenames as $filename) {
//Get a JSON file from the list
$arrayTwo = file_get_contents($filename);
$arrayTwo = json_decode($arrayTwo);
//Here's where I'm trying to merge the original and the new
//overwriting the initial variable created outside the loop, to be used again
$arrayThree = array_merge_recursive($arrayThree, $arrayTwo);
}
//Encode again as JSON, save out and empty
$arrayThree = json_encode($arrayThree);
file_put_contents("newMerge2.json", $arrayThree);
unset($arrayThree);//release memory