Reputation: 7
Here I show an example of what I am currently using, it is working but not efficiently enough. I have been stumped for a couple of days now on trying to find an effective alternative but I lack the knowledge.
I have 2 JSON files, both share the same keys but the values change.
$file_data = file_get_contents('./data_1.json');
$json_data_1 = json_decode($file_data, true);
$file_data = file_get_contents('./data_2.json');
$json_data_2 = json_decode($file_data, true);
foreach($json_data_1["items"] as $key_1=>$val_1){
foreach($json_data_2["items"] as $key_2=>$val_2){
if ($val_1['guy'] == $val_2['guy']) {
echo "Match Found!";
// here I check for any differences
// in other values
break(1);
}
}
}
Upvotes: 0
Views: 68
Reputation: 1511
$file_data = file_get_contents('./data_1.json');
$json_data_1 = json_decode($file_data, true);
$file_data = file_get_contents('./data_2.json');
$json_data_2 = json_decode($file_data, true);
print_r(array_diff($json_data_1["items"],$json_data_2["items"]);
https://php.net/manual/en/function.array-diff.php
Upvotes: 1
Reputation: 2185
since it users the thae same key's you only need to use one foreach loop and use the key.
try this code:
$file_data = file_get_contents('./data_1.json');
$json_data_1 = json_decode($file_data, true);
$file_data = file_get_contents('./data_2.json');
$json_data_2 = json_decode($file_data, true);
foreach($json_data_1["items"] as $key_1=>$val_1){
if ($val_1['guy'] == $json_data_2['items'][$key_1]['guy']) {
echo "Match Found!";
// here I check for any differences
// in other values
break(1);
}
}
}
Upvotes: 0