31

I have two arrays that both look like this:

Array
(
    [0] => Array
        (
            [name] => STRING
            [value] => STRING
        )

    [1] => Array
        (
            [name] => STRING
            [value] => STRING
        )

    [2] => Array
        (
            [name] => STRING
            [value] => STRING
        )
)

and I would like to be able to replicate array_intersect by comparing the ID of the sub arrays within the two master arrays. So far, I haven't been successful in my attempts. :(

Nathan
  • 2,699
  • 5
  • 30
  • 44
  • 3
    Doing a custom comparison, you'll need to use `array_uintersect()` http://php.net/array_uintersect, which uses a user-defined comparison function. – Wiseguy Apr 13 '11 at 17:47
  • Despite all of the upvotes on this old post, your question does not contain a good, clear [mcve]. – mickmackusa Jan 24 '22 at 22:38
  • [Compare two 2D arrays & get intersection and differences](https://stackoverflow.com/q/37564953/2943403) – mickmackusa Feb 24 '22 at 05:03

3 Answers3

57

Use array_uintersect() to use a custom comparison function, like this:

$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']);
}

which yields, as you would hope:

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

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

)
Wiseguy
  • 20,522
  • 8
  • 65
  • 81
  • How could you modify this to make sure all columns match, not just 'value'? – bafromca Jan 20 '16 at 08:41
  • 2
    @bafromca I think return strcmp(serialize($val1), serialize($val2)); might work – ChrisS Jun 15 '16 at 13:49
  • better use a recursive funktion like: `function compare_arrays($a, $b) { if (is_array($a) && is_array($b)) { $diff = array_udiff($a, $b, 'compare_arrays'); return empty($diff) ? 0 : 1; } return ($a < $b) ? -1 : 1; } $result = array_uintersect($a, $b, 'compare_arrays');` – rubo77 Apr 08 '23 at 17:26
3
function compareDeepValue($val1, $val2)
{
   return strcmp($val1['value'], $val2['value']);
}

Be sure that val2 key is existing in val1 array, because the function is ordering array first. Very strange.

smottt
  • 3,272
  • 11
  • 37
  • 44
ScreamZ
  • 39
  • 1
3

you can use embedded function with array_uintersect php function. ex:

$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)
rüff0
  • 906
  • 1
  • 12
  • 26