1

I have to find the elements which exist in multiple arrays,

For example, I have 4 arrays

Array1 = {P1,P2,P3,P4}
Array2 = {P1,P2,P3}
Array3 = {P1}
Array4 = {P2,P3}

I have to find elements which exist in all the arrays i.e for the above example the elements im expecting is {P1,P2} or {P1,P3}

How can i do this using PHP array functions?

Please help.

3

2 Answers 2

2

use the array_intersect function of php

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

3 Comments

I thought array_intersect will give only element(s) which are present in all the arrays! Isn't it?
@srinath: I have to find the common elements in multiple arrays. I have to find elements which exist in all the arrays. So what do you really wants? If this is not your question then what is your question?
Edited my question, "I have to find elements which exist in all the arrays" is THE question. I think the example should make it clear..
0

There is a built in PHP function that will do what you're asking for called array_intersect

http://www.php.net/manual/en/function.array-intersect.php

1 Comment

I thought array_intersect will give only element(s) which are present in all the arrays! Isn't it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.