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
    )
1
  • Use foreach(){foreach(){}} to compare Commented Aug 27, 2014 at 7:14

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

2

Try array_intersect to find the common element of any array.

$result = array_intersect($array[0],$array[1],$array[2])

Comments

-1

Try using PHP's own function array_instersect()

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.