phpmultidimensional-arrayfilteringassociative-arrayarray-difference

Get the difference between two 2-dimensional arrays (each containing a single column of values)


I have an array containing rows of associative data.

$array1 = array(
    array('ITEM' => 1),
    array('ITEM' => 2),
    array('ITEM' => 3),
);

I have a second array, also containing rows of associative data, that I would like to filter using the first array.

$array2 = array(
    array('ITEM' => 2),
    array('ITEM' => 3),
    array('ITEM' => 1),
    array('ITEM' => 4),
);

This feels like a job for array_diff(), but how can I compare the rows exclusively on the deeper ITEM values?

How can I filter the second array and get the following result?

array(3 => array('ITEM' => 4))

Solution

  • I would probably iterate through the original arrays and make them 1-dimensional... something like

    foreach($array1 as $aV){
        $aTmp1[] = $aV['ITEM'];
    }
    
    foreach($array2 as $aV){
        $aTmp2[] = $aV['ITEM'];
    }
    
    $new_array = array_diff($aTmp1,$aTmp2);