4

I have 4 arrays, which are going to be different every time I run my code. Sometimes, maybe empty. I am using array_intersect to get the common value from the set of arrays and if any one of the array is empty it returns me an empty array. I want array_intersect to ignore empty array's.

Example:

$array1 = array('441', '442', '443');
$array2 = array('441', '443');
$array3 = array('441', '442',);
$array4 = array();

var_dump(array_intersect($array1, $array2, $array3, $array4));

// Returns
array('');

// Required
array('441');

How do I make it ignore $array4 and give an output of 441

4
  • 1
    This is how the function works. And everything else would not make sense. You need to check if your array is empty before you call the function - and then either include it in the list of parameters, or not.
    – CBroe
    Oct 4, 2017 at 12:38
  • 1
    I know, but that what I exactly asked in the question. How do I make array_intersect ignore the empty array Oct 4, 2017 at 12:38
  • Is there always only 4 arrays? Oct 4, 2017 at 12:39
  • Yea always 4 but any one could be empty Oct 4, 2017 at 12:40

4 Answers 4

15

If you store your arrays as an array you can do this:

<?php
// create array of arrays
$array[] = array('441', '442', '443');
$array[] = array('441', '443');
$array[] = array('441', '442');
$array[] = array();

// filter out empty array
$array = array_filter($array);

// pass in array (inline argument unpacking PHP > 5.6)
print_r(array_intersect(...$array));

Array
(
    [0] => 441
)

https://3v4l.org/MD3r2

0
2
$tempArray = [];
if (count($g_arr) >0) $tempArray[] = $g_arr;
if (count($c_arr) >0) $tempArray[] = $c_arr;
if (count($k_arr) >0) $tempArray[] = $k_arr;
if (count($m_arr) >0) $tempArray[] = $m_arr;
if (count($p_arr) >0) $tempArray[] = $p_arr;

$intersect = call_user_func_array('array_intersect', $tempArray);
2
  • That's a bad practice I think ! Oct 4, 2017 at 12:44
  • Why count()? You could just check if it's empty(), and these variable names are not descriptive in the slightest Oct 4, 2017 at 12:45
2

Have a look on below solution:

$array = array(); //initialize $arrar
$array[] = array('441', '442', '443');
$array[] = array('441', '443');
$array[] = array('441', '442',);
$array[] = array();
$array = array_filter($array); //filter empty array
$res_arr = array_shift($array); // Shift an element off the beginning of array
foreach($array as $filter){
        $res_arr = array_intersect($res_arr, $filter);
}

print_r($res_arr);

Output

Array ( [0] => 441 )

See example at: https://3v4l.org/Rkt1R

1
$array1 = array('441', '442', '443');
$array2 = array('441', '443');
$array3 = array('441', '442',);
$array4 = array();


function get_intersect(...$arrays){
    $instersect_arrays = array();

    foreach($arrays as $array){
        if(!empty($array)){
            array_push($instersect_arrays,$array);
        }
    }

    return call_user_func_array('array_intersect', $instersect_arrays);
}

var_dump(get_intersect($array1, $array2, $array3, $array4));

//output: array(1) { [0]=> string(3) "441" } 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.