[英]PHP 2 arrays - merge values existing in both arrays to one array
I have 2 arrays: 我有2个数组:
$array1 = array(1,2,3,4,5);
$array2 = array(3,4,5,6,7);
Is there any PHP function that does this? 是否有任何PHP功能可以做到这一点?
$finalArray = unknown_php_function($array1,$array2);
// result: $finalArray = array(3,4,5);
It merges both arrays and removes values which aren't present in both arrays. 它合并两个数组并删除两个数组中不存在的值。 Do I have to build a foreach cycle or is there an easier way?
我是否必须构建一个foreach循环或者是否有更简单的方法? Thanks
谢谢
You want array_intersect
for this, basically the intersection of two sets (arrays, in this case), just like in school. 你想要
array_intersect
,基本上是两组(在这种情况下是数组)的交集,就像在学校一样。 :-) :-)
You're looking for array_intersect()
. 您正在寻找
array_intersect()
。 Here is a demo: 这是一个演示:
$array1 = array(1,2,3,4,5);
$array2 = array(3,4,5,6,7);
$finalArray = array_intersect($array1,$array2);
print_r($finalArray);
Outputs: 输出:
Array
(
[2] => 3
[3] => 4
[4] => 5
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.