例如我有两个数组,$arrayA = [0=>"A", 1=>"B", 2=>"C", 3=>"C", 4=>"E", 5=>"F"];$arrayB = [0=>"B", 1=>"C"];如果我使用array_intersect():$same = array_intersect($arrayA, $arrayB);我会得到$same的结果1 => B2 => C3 => C相反,我只想得到1 => B2 => C更新在尝试了amow和ekin提供的答案之后,我意识到这种方法是有局限性的。如果我的数组是下面的形式,$arrayA = [0=>"A", 1=>"B", 2=>"C", 3=>"B", 4=>"C", 5=>"E", 6=>"F"];$arrayB = [0=>"B", 1=>"C", 2=>"B", 3=>"C"];$same = array_unique(array_intersect($arrayA, $arrayB));$same的结果仍然是1 => B,2 => C我想去的地方1 => B,2 => C,3 => B,4 => C 最佳答案 希望这能有所帮助。array_unique doc带更新的解决方案:$arrayA = [0=>"A", 1=>"B", 2=>"C", 3=>"B", 4=>"C", 5=>"E", 6=>"F"];$arrayB = ["B","C","B","C"];$filter = $arrayB;$same = [];foreach ($arrayA as $key => $value) { $bIndex = array_search($value, $filter); if($bIndex !== false){ $same[$key] = $value; unset($filter[$bIndex]); }}var_dump($same);关于php - PHP比较数组元素作为一个整体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40120897/ 10-09 18:45