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.

srinath
  • 166
  • 2
  • 5
  • This seems to be what you're looking for: http://php.net/manual/en/function.array-intersect.php – Quasdunk Nov 23 '11 at 15:07
  • 1
    Dup of [Find common values in multiple arrays with PHP](http://stackoverflow.com/questions/5299608/find-common-values-in-multiple-arrays-with-php) – outis Nov 23 '11 at 15:12
  • I thought array_intersect will give only element(s) which are present in all the arrays! Isn't it? – srinath Nov 23 '11 at 15:16

2 Answers2

2

use the array_intersect function of php

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • I thought array_intersect will give only element(s) which are present in all the arrays! Isn't it? – srinath Nov 23 '11 at 15:26
  • @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? – Shakti Singh Nov 23 '11 at 15:35
  • 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.. – srinath Nov 23 '11 at 16:06
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

Mike Almond
  • 166
  • 5
  • I thought array_intersect will give only element(s) which are present in all the arrays! Isn't it? – srinath Nov 23 '11 at 15:16