Using array_intersect on a multi-dimensional array



PHP Snippet 1:

$arr1 = array(
           array('name' => 'asdfjkl;', 'value' => 'foo'),
           array('name' => 'qwerty', 'value' => 'bar'),
           array('name' => 'uiop', 'value' => 'baz'),
        );

$arr2 = array(
           array('name' => 'zxcv', 'value' => 'stuff'),
           array('name' => 'asdfjkl;', 'value' => 'foo'),
           array('name' => '12345', 'value' => 'junk'),
           array('name' => 'uiop', 'value' => 'baz'),
        );

$intersect = array_uintersect($arr1, $arr2, 'compareDeepValue');
print_r($intersect);

function compareDeepValue($val1, $val2)
{
   return strcmp($val1['value'], $val2['value']);
}

PHP Snippet 2:

Array
(
    [0] => Array
        (
            [name] => asdfjkl;
            [value] => foo
        )

    [2] => Array
        (
            [name] => uiop
            [value] => baz
        )

)

PHP Snippet 3:

function compareDeepValue($val1, $val2)
{
   return strcmp($val1['value'], $val2['value']);
}

PHP Snippet 4:

$intersectNames = array_uintersect($arr1, $arr2, function ($val1, $val2){
    return strcmp($val1['name'], $val2['name']);
    });

$intersectValues = array_uintersect($arr1, $arr2, function ($val1, $val2){
    return strcmp($val1['value'], $val2['value']);
    });

print_r('namesIntersected' => $intersectNames, 'valuesIntersected' => $intersectValues)