PHP:如何从不同的数组中获取常见值?

时间:2016-11-22 09:50:12

标签: php arrays

我有两个数组如下: -

$a = ["2","11","6"];
$b = ["6","7"];
$c = array_diff($a, $b);

$c = ["2","11"];

$c中的结果是错误的。我希望结果应为$c = [6]

换句话说,我希望返回两个数组中的公共元素!但它给出了错误的错误。请帮助我?

4 个答案:

答案 0 :(得分:2)

使用array_intersect()

$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a, $b);

演示:https://eval.in/682653

答案 1 :(得分:2)

您可以使用array_intersect

$c = array_intersect($a, $b);

答案 2 :(得分:2)

$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a,$b);

答案 3 :(得分:0)

使用array_intersect(http://php.net/manual/en/function.array-intersect.php

<?php
$a = ["2","11","6"];
$b = ["6","7"];
$c = array_intersect($a, $b);

print_r($c)

?>