Php 比较2个JSON对象,删除重复项

Php 比较2个JSON对象,删除重复项,php,json,Php,Json,我需要能够比较2个JSON对象,如: $json1 = json_encode('["red", "green", "blue", "white"]'); $json2 = json_encode('["bla", "something", "haha", "blue"]'); 现在,比较这两个对象,值“blue”是重复的,应该从$json2中删除 我该怎么做呢?如果您已经将JSON解码成PHP数组JSON\u decode($json1,true),那么: 查找重复项: $dupes = a

我需要能够比较2个JSON对象,如:

$json1 = json_encode('["red", "green", "blue", "white"]');
$json2 = json_encode('["bla", "something", "haha", "blue"]');
现在,比较这两个对象,值“blue”是重复的,应该从
$json2
中删除


我该怎么做呢?

如果您已经将JSON解码成PHP数组
JSON\u decode($json1,true)
,那么:

查找重复项:

$dupes = array_intersect($json1, $json2);
//to remove from $json1
$json1 = array_diff($json1, $dupes);

//to remove from $json2
$json2 = array_diff($json2, $dupes);
获取差异(无重复项):

只需从
$json2
中删除,就更容易了:

$json2 = array_diff($json2, $json1);