我想比较 php 中的 2 个 json 文件(a.json,b.json)。
文件 a.json 和 b.json 将包含 json 数组。
文件 a.json 和 b.json 将具有不同的对齐方式,但 json 数组数据将保持不变
上面是否有任何库。
有人可以帮我解决这个问题吗?
最佳答案
您可以对这两个文件使用 PHP 函数 json_decode()
将 json 数组解码为普通数组,然后您可以使用 array_diff_key()
。最后一个函数通过键返回两个数组的差异,如果结果为空则您的文件相等。
$arrayA = json_decode(file_get_contents('path/to/a.json'),true);
$arrayB = json_decode(file_get_contents('path/to/b.json'),true);
$result = array_diff_key($arrayA,$arrayB);
if(empty($result)){
//Same file
}else{
//Differences occured
}
让我知道是否成功
关于php - 比较php中的2个json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50910125/