Search code examples
phparrayslaravelarray-intersect

how to split arrays inside a big array to fit intersect function?


I have the following array that contains other arrays:

$bigArray = [$array_one, $array_two, $array_three,.... ];

I want to array_intersect the inner arrays like so:

$intersect = array_intersect($array_one, $array_two, $array_three,....);

How do I handle it?


Solution

  • Like this:

    $intersect = array_intersect(...$bigArray);
    

    The ... operator, introduced in PHP 5.6, allows you to use an array to pass multiple function arguments.

    It's also possible to do this with call_user_func_array, but argument unpacking offers some advantages over that approach.