简体   繁体   English

比较php中的2个json文件

[英]Compare 2 json files in php

I wish to compare 2 json files( a.json , b.json) in php . 我希望在php中比较2个json文件(a.json,b.json)。

Files a.json and b.json will have array of json in it . 文件a.json和b.json将包含json数组。

Files a.json and b.json will have different alignment but the json array data will remains same 文件a.json和b.json将具有不同的对齐方式,但json数组数据将保持不变

Is there any libraries for the above. 上面有没有任何库。

can any on help me out on this ? 有没有可以帮我解决这个问题?

You can decode json array to a normal array using the PHP function json_decode() for both the files and then you can use array_diff_key() . 您可以使用PHP函数json_decode()将json数组解码为普通数组,然后您可以使用array_diff_key() This last function return the difference by key of the two arrays, if the result is empty then your files are equal. 最后一个函数返回两个数组的键差异,如果结果为空则则文件相等。

$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
}

Let me know if it made the trick 让我知道它是否成功了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM