You can use array_filter() and a custom implementation of in_array() to check if your array is null
Solution :
$array1 = array(6, 10, 11, 12);
$array2 = array(6, 741, 18, 9, 110, 11, 12);
$array3 = array(8, 10, 11, 20);
$array4 = null;
$array5 = array(9, 10, 11, 12);
function in_array_custom($item, $array)
{
if($array === null){
return true;
}
return in_array($item, $array);
}
function intersect($item)
{
global $array2;
global $array3;
global $array4;
global $array5;
return in_array_custom($item, $array2) && in_array_custom($item, $array3) && in_array_custom($item, $array4) && in_array_custom($item, $array5);
}
print_r(array_filter($array1, "intersect"));
Live example
I share the global solution :
<?php
$arrays = array(
array(6, 10, 11, 12),
array(6, 741, 18, 9, 110, 11, 12),
array(8, 10, 11, 20),
null,
array(9, 10, 11, 12)
);
function in_array_custom($item, $array)
{
if($array === null){
return true;
}
return in_array($item, $array);
}
function in_arrays($item, $arrays)
{
foreach($arrays as $array)
{
if(!in_array_custom($item, $array)) {
return false;
}
}
return true;
}
function intersect($item)
{
global $arrays;
return in_arrays($item, $arrays);
}
print_r(array_filter($arrays[0], "intersect"));
Live example
Here, there is one little issue, that If the first array ($array1
) is null, then the code will not work, but that issue can be resolved by taking $array1
as a Union of all the SIX Arrays, (5 Arrays and 1 Union Array), And the Data is shifted to the next array, i.e $array2
now holds the data of $array1
, $array3 = $array2
and so on...
P.S. - Union of Arrays can be done like this $array1 = $array2 + $array3 + $array4 + $array5 + $array6;