$jsonArray1 = '["7.00 am To 8.00 am","1.00 pm To 2.00 pm","2.00 pm To 3.00 pm"]';
$jsonArray2 = '["7.00 am To 8.00 am","1.00 pm To 2.00 pm","2.00 pm To 3.00 pm","10.00 am To 11.00 am"]';
答案 0 :(得分:1)
首先使用json_decode将json字符串转换为数组 和USE array_diff找到不同的值
这是代码 -
$arr1 = json_decode($jsonArray1);
$arr2 = json_decode($jsonArray2);
$newarr = array_diff($arr2, $arr1);
print_r($newarr); // php array format
json_encode($newarr); // json format
答案 1 :(得分:1)
试试这个:
//you have to decode arrays
$json1 = json_decode( $jsonArray1, true);
$json2 = json_decode( $jsonArray2, true);
//find duplicates
$find_duplicates = array_intersect($json1, $json2);
//remove duplicates from first array
$json1 = array_diff($json1, $find_duplicates);
///remove duplicates from second array
$json2 = array_diff($json2, $find_duplicates);
答案 2 :(得分:1)
使用array_intersect
和array_filter
函数的解决方案:
$json_str1 = '["7.00 am To 8.00 am","1.00 pm To 2.00 pm","2.00 pm To 3.00 pm"]';
$json_str2 = '["7.00 am To 8.00 am","1.00 pm To 2.00 pm","2.00 pm To 3.00 pm","10.00 am To 11.00 am"]';
list($arr1, $arr2) = [json_decode($json_str1), json_decode($json_str2)];
$common_items = array_intersect($arr2, $arr1);
$result = array_filter(array_merge($arr1, $arr2), function($v) use($common_items){
return !in_array($v, $common_items);
});
print_r($result);
输出:
Array
(
[6] => 10.00 am To 11.00 am
)