使用 PHP 在多个数组中查找公共值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5299608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find common values in multiple arrays with PHP
提问by mspir
I need to find common values in multiple arrays. Number of arrays may be infinite.
Example (output from print_r
)
我需要在多个数组中找到公共值。数组的数量可能是无限的。示例(来自 的输出print_r
)
Array1
(
[0] => 118
[1] => 802
[2] => 800
)
Array2
(
[0] => 765
[1] => 801
)
Array3
(
[0] => 765
[1] => 794
[2] => 793
[3] => 792
[4] => 791
[5] => 799
[6] => 801
[7] => 802
[8] => 800
)
now, I need to find the values that are common on all 3 (or more if available) of them.... how do I do that?
现在,我需要找到所有 3 个(或更多,如果可用)的共同值......我该怎么做?
Thanx
谢谢
回答by Mark Baker
$intersect = array_intersect($array1,$array2,$array3);
If you don't know how many arrays you have, then build up an array of arrays and user call_user_func_array()
如果你不知道你有多少个数组,那么建立一个数组的数组和用户 call_user_func_array()
$list = array();
$list[] = $array1;
$list[] = $array2;
$list[] = $array3;
$intersect = call_user_func_array('array_intersect',$list);