Search code examples
phpmultidimensional-arrayfilteringintersectionvariadic

autofill arguments in function with items from array


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?


Solution

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

    call_user_func_array('array_intersect', $list);
    
    array_intersect(...$list);