我需要比较两个有一个公用密钥的不同数组。
我编写了一个函数,该函数可以满足我的需要,但是我想知道是否有最快,最简单的方法来获得相同的结果。
这是我的代码:
数组示例:
第一个数组:
Array
(
[0] => Array
(
[dafid] => daf_60304
[title] => Hansel & Gretel - Cacciatori di streghe
[creation] => 2018-09-01 00:02:25
)
[1] => Array
(
[dafid] => daf_115
[title] => Il grande Lebowski
[creation] => 2018-09-01 00:02:25
)
[2] => Array
(
[dafid] => daf_238636
[title] => Anarchia - La notte del giudizio
[creation] => 2018-09-01 00:02:25
)
这里是第二个数组:
Array
(
[0] => Array
(
[dafid] => daf_316727
[title] => La notte del giudizio - Election Year
[creation] => 2018-09-01 00:02:25
)
[1] => Array
(
[dafid] => daf_115
[title] => Il grande Lebowski
[creation] => 2018-09-01 00:02:25
)
[2] => Array
(
[dafid] => daf_209112
[title] => Batman v Superman: Dawn of Justice
[creation] => 2018-09-01 00:02:25
)
结果:
Array
(
[0] => Array
(
[dafid] => daf_60304
[title] => Hansel & Gretel - Cacciatori di streghe
[creation] => 2018-09-01 00:02:25
)
[1] => Array
(
[dafid] => daf_238636
[title] => Anarchia - La notte del giudizio
[creation] => 2018-09-01 00:02:25
)
结果应该是一个数组,其中不包含具有相同[dafid]的元素。 我得到了执行此功能的结果,但我正在寻找更干净的解决方案。
function removeAlreadyDAF($array1,$array2) {
$dafNextMovies = $array1;
$dafDB = array2;
foreach ($dafDB as $k => $v) {
//$v['dafid']; //id release
foreach ($dafNextMovies as $key => $value) {
//loop release disponibili
if ($value['dafid'] == $v['dafid']) {
unset($dafNextMovies[$key]);
}
}
}
$dafNextMovies = array_values($dafNextMovies);
return $dafNextMovies;
}
答案 0 :(得分:2)
检查此功能:array_udiff
第三个参数如何选择element。应该是这样的。评论中有一个例子。选中它(功能compare_names($ a,$ b))
private function compareData($a, $b) {
return strcmp($a['name'], $b['name']);
}
答案 1 :(得分:0)
您可以轻松地从数组相交中创建一个新数组
$result_array = array_intersect_assoc($array1, $array2);
这正是您想要的。