Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare 2 json files in php

Tags:

json

php

I wish to compare 2 json files( a.json , b.json) in php .

Files a.json and b.json will have array of json in it .

Files a.json and b.json will have different alignment but the json array data will remains same

Is there any libraries for the above.

can any on help me out on this ?

like image 285
soundararajan.c Avatar asked Sep 26 '26 14:09

soundararajan.c


1 Answers

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(). 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

like image 134
Lucarnosky Avatar answered Sep 28 '26 03:09

Lucarnosky