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,694
  • 12
  • 53
  • 70
Nomad
  • 613
  • 2
  • 7
  • 15

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
  • 917
  • 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
  • 125
  • 9
-1

Try using PHP's own function array_instersect()

RichardBernards
  • 3,146
  • 1
  • 22
  • 30
  • Rushing an answer that merely points to the manual is not as helpful as actually demonstrating the technique (which other answers have done). When you just want to drop a link to the manual, post a comment under the question instead of posting an answer. – mickmackusa Oct 20 '22 at 21:27