0

I have four arrays and I want to get the common elements of each array. Is there a function that will allow me to compare multiple arrays and get their common element?

[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
    )
Kevin
  • 41,329
  • 12
  • 52
  • 68
Nomad
  • 583
  • 2
  • 5
  • 14

3 Answers3

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

Reference Here

Community
  • 1
  • 1
Coder anonymous
  • 923
  • 1
  • 8
  • 25
2

Try array_intersect to find the common element of any array.

$result = array_intersect($array[0],$array[1],$array[2])
NMN
  • 115
  • 9
0

Try using PHP's own function array_instersect()

RichardBernards
  • 3,158
  • 1
  • 21
  • 30