如何在 PHP 中检测两个 JSON 对象之间的重复元素


how to detect a duplicate element between two json object in php

我试图像这样检测 2 个 JSON 对象之间的不同元素;

//Json1    
    [
        {"file":"arrowssss.png"},
        {"file":"arrows.png"},
        {"file":"logo.png"}
    ]
//Json1    
    [
        {"file":"arrows.png"},
        {"file":"logo.png"}
    ]

我需要返回箭头.png。

有什么建议吗?

尝试

function flatten(array $array) {
    $return = array();
    array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
    return $return;
}
$json1 = '[{"file":"arrowssss.png"},
        {"file":"arrows.png"},
        {"file":"logo.png"}]';
$json2 = '[{"file":"arrows.png"},
        {"file":"logo.png"}]';
$array1 = flatten(json_decode($json1,true));
$array2 = flatten(json_decode($json2,true));
print_r(array_diff($array1,$array2));

结果:-

Array ( [0] => arrowssss.png ) 
$json1='[
    {"file":"arrowssss.png"},
    {"file":"arrows.png"},
    {"file":"logo.png"}
]';
$json2 = '[
    {"file":"arrows.png"},
    {"file":"logo.png"}
]';
function getFile($key)
{
    return isset($key['file']) ? $key['file'] : null;
}
$diff = array_diff(array_map('getFile', json_decode($json1, true)), array_map('getFile', json_decode($json2, true)));
print_r($diff);

结果:-

Array ( [0] => arrowssss.png )