我有四个数组,我想获得每个数组的公共元素。是否有一个函数可以让我比较多个数组并得到它们的共同元素?
[0] => Array
(
[0] => 121186
[1] => MPE129
[2] => MHB1
[3] => 60000
[4] => 2014-2015
[5] => 1
)
[1] => Array
(
[0] => 102147
[1] => MPE129
[2] => MHB1
[3] => 60000
[4] => 2014-2015
[5] => 1
)
[2] => Array
(
[0] => 130879
[1] => MPE129
[2] => MHB1
[3] => 60000
[4] => 2014-2015
[5] => 1
)
[3] => Array
(
[0] => 101768
[1] => MPE129
[2] => MHB1
[3] => 60000
[4] => 2014-2015
[5] => 1
)
答案 0 :(得分:3)
array_intersect()
$intersect = array_intersect($array1,$array2,$array3);
如果你不知道你有多少个数组,那么就建立一个数组数组和用户call_user_func_array()
$list = array();
$list[] = $array1;
$list[] = $array2;
$list[] = $array3;
$intersect = call_user_func_array('array_intersect',$list);
参考Here
答案 1 :(得分:2)
尝试使用array_intersect查找任何数组的公共元素。
$result = array_intersect($array[0],$array[1],$array[2])
答案 2 :(得分:0)
尝试使用PHP自己的函数array_instersect()