There is a need from several arrays to get the values ​​that are in all these arrays. for example

$array1 = [1,3,6,7]; $array2 = [63,34,1,2]; $array3 = [1,7,5,2]; $array4 = []; 

The result is an empty array. If there were no last array $array4 , then the search result would be 1, since it is in all arrays.

As it turned out , you can quickly and conveniently solve the problem using array_intersect() . However, there is a big problem - I don’t know exactly how many arrays need to be compared. They can be from 0 to 7.

For me it is important to find the convergence of only declared arrays. If the array was not created, then it does not need to be included.

for example

 $a = rand(true, false); $b = rand(true, false); if ($a) { $array1 = [1,3,5,7]; } if ($b) { $array2 = []; } $array3 = [1,8,99]; 

As a result, sometimes there is an empty array arr2 , and sometimes it is not. If this array is declared, then the result of searching for the value that is in all arrays is an empty array. If the arr2 array arr2 not declared, the result will be different - 1.

I can get an array of declared arrays like this:

 $all = [ $array1 ?? null, $array2 ?? null, $array3 ?? null, ]; foreach ($all as $key => $item) { if ($item === null) { unset($all[$key]); } } 

As a result, I have an array with arrays, among the values ​​of which I need to find such values ​​that are present in each of these arrays. But how can this be done?

  • Nonsense but worker =) use eval =) - Vladimir Klykov

1 answer 1

I'll answer you here call_user_func_array : use call_user_func_array or ... (starting with php5.6):

 array_intersect(...$all); // c php5.6 call_user_func_array('array_intersect', $all); // до php5.6