0

I have code that for every client $client I end up with an array of business IDs $ids[]. I can have at any point in the code I can have up to 6 clients in this function, the clients are stored in their own array inside a client object.

What I am trying to figure out how to do is, without knowing how many clients are in the object at any given run of the script, how do I find the intersection of the business IDs so I can get the list of IDs that appear in ALL client records?

I know I can use array_intersect($arr1, arr2, ...) and so on, but that requires me to know beforehand which arrays I am comparing, when in this case the arrays are generated on the fly, so I can't manually add the $id arrays to array_intersect().

I'm stumped to figure out how to find the intersection of all of the arrays. Is there a way to pass in a function as the argument for array_intersect()? Maybe as a closure? If so, how would I accomplish that?

1 Answers1

1

I think you can use splat operator

<?php

$clients = [
    [1,2,3],
    [1,5,6],
    [1,7,8],
];

var_dump(array_intersect(...$clients));
array(1) {
  [0]=>
  int(1)
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92