在比较两个数组时,我找到了不同的答案,但在我的例子中没有一个起作用。我有两个Jsons,一个保存旧值的Old.json和要保存新值的New.json。我只想把New.json中还没有的新东西保存在Old.json中
OLD.JSON
{
"jogos-da-copa": [
"/videos/291856.html",
"/videos/291830.html",
"/videos/291792.html",
"/videos/291759.html",
"/videos/291720.html",
"/videos/291705.html"
],
"apresentacao": [
"/videos/2926328.html",
"/videos/67.html",
"/videos/36.html",
"/videos/3.html"
]
}NEW.JSON
{
"jogos-da-copa": [
"/videos/291887.html",
"/videos/291856.html",
"/videos/291830.html",
"/videos/291792.html",
"/videos/291759.html",
"/videos/291720.html",
"/videos/291705.html"
],
"apresentacao": [
"/videos/2926385.html",
"/videos/2926328.html",
"/videos/67.html",
"/videos/36.html",
"/videos/3.html"
]
}我使用了这段代码,但它没有显示这些差异。
$old1 = json_decode(file_get_contents('old.json'), true);
$new2 = json_decode(file_get_contents('new.json'), true);
$test = [];
foreach ($old1 as $key1 => $olds1) {
foreach ($new2 as $key2 => $news2 ) {
$test[] = array_diff($olds1, $news2);
}
}
var_dump($test);发布于 2018-05-10 04:13:57
来自比较文档:
将array1与一个或多个其他数组进行比较,并返回任何其他数组中不存在的array1中的值。
在您的示例中,新数组包含来自旧数组的所有值。要获得所有新值的列表,您需要将参数转换为:
$old1 = json_decode(file_get_contents('old.json'), true);
$new2 = json_decode(file_get_contents('new.json'), true);
$test = [];
foreach ($old1 as $key1 => $olds1) {
foreach ($new2 as $key2 => $news2 ) {
$test[] = array_diff($news2, $olds1);
}
}
var_dump($test);https://stackoverflow.com/questions/50265222
复制相似问题