-1

I need to compare all first levels items of a muli-dimensional array and get the intersection values.. But the array doesn't have a fixed number arrays to compare with each other..

Here you have to explicit type each argument in array_intersect..

$list = [
  [0,1,2],
  [2,5],
  [-1,2]
];

$t = array_intersect($list[0], $list[1], $list[2]);
print_r($t);

But what if the $list array had 10 sub-arrays and I wanted to compare each and one of them?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
clarkk
  • 27,151
  • 72
  • 200
  • 340
  • Do you want this effect? https://stackoverflow.com/questions/12381085/array-intersect-a-variable-amount-of-arrays https://stackoverflow.com/questions/39398040/php-dynamic-array-intersect https://stackoverflow.com/questions/38056228/get-intersection-of-a-multiple-array-in-php – mickmackusa Mar 30 '18 at 12:59
  • More: https://stackoverflow.com/questions/43710980/array-intersect-with-dynamic-length-of-arguments https://stackoverflow.com/questions/14291340/array-intersect-inside-multidimensional-array https://stackoverflow.com/questions/9438057/array-intersect-but-for-a-sub-arrays-of-a-single-array-variable https://stackoverflow.com/questions/47201552/how-would-i-use-array-intersect-with-the-arrays-in-an-array https://stackoverflow.com/questions/22449442/php-array-intersect-making-issues-and-im-not-able-to-check-the-empty-array – mickmackusa Mar 30 '18 at 13:07
  • https://stackoverflow.com/questions/29031430/how-to-get-array-intersect-with-single-array-group https://stackoverflow.com/questions/37345560/php-comparing-and-extracting-common-array-elements – mickmackusa Mar 30 '18 at 13:11

2 Answers2

3

You can use call_user_func_array (< 5.6) or arguments unpacking (>= 5.6)

call_user_func_array('array_intersect', $list);

array_intersect(...$list);
Justinas
  • 41,402
  • 5
  • 66
  • 96
2

Try using call_user_func_array:

call_user_func_array('array_intersect', $list);
hsz
  • 148,279
  • 62
  • 259
  • 315