开发者

Find common values in multiple arrays with PHP

开发者 https://www.devze.com 2023-02-17 04:30 出处:网络
I need to find common values in multiple arrays. Number of arrays may be infinite. Example (output from print_r)

I need to find common values in multiple arrays. Number of arrays may be infinite. Example (output from 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?

Thanx


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()

$list = array();
$list[] = $array1;
$list[] = $array2;
$list[] = $array3;
$intersect = call_user_func_array('array_intersect',$list);
0

精彩评论

暂无评论...
验证码 换一张
取 消