I am trying to compare 2 json strings with each other to find all new entries in the list.
我正在尝试比较两个json字符串,以找到列表中的所有新条目。
This is how I am comparing them:
这就是我比较它们的方式:
$json = json_decode(file_get_contents("new.json"), true);
$last_json = json_decode(file_get_contents("last.json"), true);
$difference = array_diff($json, $last_json);
print_r($difference);
I am expecting it to return an array with all new entries. However, I am just getting an empty array in return.
我期望它返回一个包含所有新条目的数组。然而,我只是得到一个空数组作为回报。
Any help would be appreciated!
如有任何帮助,我们将不胜感激!
Additional information: I am also trying to compare the values of the arrays. This is how I'm trying to do that:
附加信息:我还试图比较数组的值。我就是这么做的:
foreach($json["whitelist_name"] AS $json_key => $json_val) {
foreach($last_json["whitelist_name"] AS $last_json_key => $last_json_val) {
if($json["whitelist_name"] != $last_json["whitelist_name"]) {
echo $json["whitelist_name"];
}
}
}
However, it seems that $json["whitelist_name"] is undefined
然而,$json[“whitelist_name”]似乎没有定义
1 个解决方案
#1
1
array_diff_assoc is the way to get difference of associative arrays:
array_diff_assoc是得到关联数组差异的方法:
$json = json_decode(file_get_contents("new.json"), true);
$last_json = json_decode(file_get_contents("last.json"), true);
$difference = array_diff_assoc($json, $last_json);
print_r($difference);
This small piece of code will find out if any whitelist_name is different in the new json than the old one
这一小段代码将会发现任何whitelist_name在新的json中是否与旧的不同
foreach($last_json as $key=>$value){
if($value['whitelist_name'] != $json[$key]['whitelist_name']){
// value is changed
}else{
// value is not changed
}
}
#1
1
array_diff_assoc is the way to get difference of associative arrays:
array_diff_assoc是得到关联数组差异的方法:
$json = json_decode(file_get_contents("new.json"), true);
$last_json = json_decode(file_get_contents("last.json"), true);
$difference = array_diff_assoc($json, $last_json);
print_r($difference);
This small piece of code will find out if any whitelist_name is different in the new json than the old one
这一小段代码将会发现任何whitelist_name在新的json中是否与旧的不同
foreach($last_json as $key=>$value){
if($value['whitelist_name'] != $json[$key]['whitelist_name']){
// value is changed
}else{
// value is not changed
}
}