得到 array_intersect 没有重复的计数 php

get array_intersect count without duplicates php

我有 2 个数组:

$first = array(1,1,2,3,4);
$second = array(1,2,3,4,5);

当我使用 $count = array_intersect($first, $second);count($count); 匹配时,它显示 5,导致 1 相交两次。我需要得到 4 个,这不算重复。

我怎样才能在 php 中做到这一点?

提前致谢。

很简单:首先确保你的数组是唯一的,然后应用array_intersect:

$count = count(array_intersect(array_unique($first), array_unique($second)));

3v4l.

上的重现案例

此外,在找到更好的答案之前,我想添加这些我已经弄清楚的锥体。

$result = array_intersect($first, $second);
  $diff = array_diff($result, $second);
   if(!empty($diff)) {
    $result = array_unique($result);
   }