php array_diff() 与多维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11821680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:12:49  来源:igfitidea点击:

array_diff() with multidimensional arrays

phparrays

提问by Jonathan Chan

Using array_diff(), I can compare and remove similar items, but what if I have the following arrays?

使用array_diff(),我可以比较和删除相似的项目,但是如果我有以下数组怎么办?

Array1

数组1

Array
(
    [0] => Array
        (
            [ITEM] => 1
        )

    [1] => Array
        (
            [ITEM] => 2
        )

    [2] => Array
        (
            [ITEM] => 3
        )
)

Array2

阵列2

Array
(
    [0] => Array
        (
            [ITEM] => 2
        )

    [1] => Array
        (
            [ITEM] => 3
        )

    [2] => Array
        (
            [ITEM] => 1
        )
    [3] => Array
        (
            [ITEM] => 4
        )
)

I want to filter out the similar items; result should return 4. How can I rearrange my array so that I can use array_diff()?

我想过滤掉相似的项目;结果应该返回 4。如何重新排列我的数组以便我可以使用array_diff()

采纳答案by Jason Fingar

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

回答by Wiseguy

You can define a custom comparison function using array_udiff().

您可以使用array_udiff().

function udiffCompare($a, $b)
{
    return $a['ITEM'] - $b['ITEM'];
}

$arrdiff = array_udiff($arr2, $arr1, 'udiffCompare');
print_r($arrdiff);

Output:

输出:

Array
(
    [3] => Array
        (
            [ITEM] => 4
        )
)

This uses and preserves the arrays' existing structure, which I assume you want.

这使用并保留了数组的现有结构,我假设您想要。

回答by Ifnot

Another fun approach with a json_encodetrick (can be usefull if you need to "raw" compare some complex values in the first level array) :

另一个有趣的json_encode技巧(如果您需要“原始”比较第一级数组中的一些复杂值,则可能很有用):

// Compare all values by a json_encode
$diff = array_diff(array_map('json_encode', $array1), array_map('json_encode', $array2));

// Json decode the result
$diff = array_map('json_decode', $diff);

回答by Mark Amery

A couple of solutions using array_filterthat are less performant than the array_udiffsolutionfor large arrays, but which are a little more straightforward and more flexible:

一些使用array_filter它的解决方案的性能不如大型阵列array_udiff解决方案,但它们更直接、更灵活:

$array1 = [
    ['ITEM' => 1],
    ['ITEM' => 2],
    ['ITEM' => 3],
];

$array2 = [
    ['ITEM' => 2],
    ['ITEM' => 3],
    ['ITEM' => 1],
    ['ITEM' => 4],
];

$arrayDiff = array_filter($array2, function ($element) use ($array1) {
    return !in_array($element, $array1);
});

// OR

$arrayDiff = array_filter($array2, function ($array2Element) use ($array1) {
    foreach ($array1 as $array1Element) {
        if ($array1Element['ITEM'] == $array2Element['ITEM']) {
            return false;
        }
    }
    return true;
});

As always with array_filter, note that array_filterpreserves the keys of the original array, so if you want $arrayDiffto be zero-indexed, do $arrayDiff = array_values($arrayDiff);after the array_filtercall.

与往常一样array_filter,请注意array_filter保留原始数组的键,因此如果您想要$arrayDiff零索引,请$arrayDiff = array_values($arrayDiff);array_filter调用后执行。

回答by shiyani suresh

you can use below code to get difference

您可以使用以下代码来获得差异

$a1 = Array(
    [0] => Array(
            [ITEM] => 1
        )
    [1] => Array(
            [ITEM] => 2
        )
    [2] => Array(
            [ITEM] => 3
        )
    );

$a2 = Array(
    [0] => Array(
            [ITEM] => 2
        )
    [1] => Array(
            [ITEM] => 3
        )

    [2] => Array(
            [ITEM] => 1
        )
    [3] => Array(
            [ITEM] => 4
        ));

array_diff(array_column($a1, 'ITEM'), array_column($a2, 'ITEM'));

回答by AFwcxx

Having the same problem but my multidimensional array has various keys unlike your "ITEM" consistently in every array.

有同样的问题,但我的多维数组在每个数组中都有不同的键,不像你的“ITEM”。

Solved it with: $result = array_diff_assoc($array2, $array1);

解决了它: $result = array_diff_assoc($array2, $array1);

Reference: PHP: array_diff_assoc

参考:PHP:array_diff_assoc

回答by Ahmed Abdullah

Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

将 array1 与一个或多个其他数组进行比较,并返回 array1 中不存在于任何其他数组中的值。

        //Enter your code here, enjoy!

$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);