使用PHP在多个数组中查找常用值

时间:2021-10-09 12:54:34

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

感谢名单

1 个解决方案

#1


51  

array_intersect()

array_intersect()

$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);

#1


51  

array_intersect()

array_intersect()

$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);