在多维数组上使用array_intersect

时间:2021-11-18 12:16:06

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. :(

我希望能够通过比较两个主数组中子数组的ID来复制array_intersect。到目前为止,我的尝试并没有成功。 :(

2 个解决方案

#1


37  

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

使用array_uintersect()来使用自定义比较函数,如下所示:

$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
        )

)

#2


4  

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.

确保val2数组中存在val2键,因为该函数首先排序数组。很奇怪。

#1


37  

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

使用array_uintersect()来使用自定义比较函数,如下所示:

$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
        )

)

#2


4  

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.

确保val2数组中存在val2键,因为该函数首先排序数组。很奇怪。