获取不同的数组值

时间:2018-12-05 12:47:07

标签: php arrays

我正在使用2个PHP数组。

print_r($matches);
print_r($dexcode_comp);

$matchesdexcode_comp

他们的结果如下:

$个匹配项:

{
    0: {
        simplicate_dexcode: "BeS712210",
        callmonkey_dexcode: "BeS712210"
    },
    1: {
        simplicate_dexcode: "BeS712210",
        callmonkey_dexcode: "BeS712210"
    },
    2: {
        simplicate_dexcode: "BeS712210",
        callmonkey_dexcode: "BeS712210"
    },
    3: {
        simplicate_dexcode: "S-T41471",
        callmonkey_dexcode: "S-T41471"
    },
    4: {
        simplicate_dexcode: "Ron35844",
        callmonkey_dexcode: "Ron35844"
    },
    5: {
        simplicate_dexcode: "Lin10961",
        callmonkey_dexcode: "Lin10961"
    },
    6: {
        simplicate_dexcode: "Tip295926",
        callmonkey_dexcode: "Tip295926"
    },
    7: {
        simplicate_dexcode: "Lin10961",
        callmonkey_dexcode: "Lin10961"
    }
},

$ dexcode_comp:

{
    0: {
        dexcode: "BeS712210"
    },
    1: {
        dexcode: "De 48245"
    },
    2: {
        dexcode: "Bis1016338"
    },
    3: {
        dexcode: "S-T41471"
    },
    4: {
        dexcode: "Ron35844"
    },
    5: {
        dexcode: "Lin10961"
    },
    6: {
        dexcode: "Tip295926"
    },
    7: {
        dexcode: "Lin10961"
    },
    8: {
        dexcode: "SLN893827"
    },
    9: {
        dexcode: "Fen1016241"
    },
    10: {
        dexcode: "Aut331661"
    },
    11: {
        dexcode: "Pro39613"
    },
    12: {
        dexcode: "Com920158"
    },
    13: {
        dexcode: "Sma21322"
    },
    14: {
        dexcode: "Aut331661"
    },
    15: {
        dexcode: "Pro39613"
    },
    16: {
        dexcode: "Com920158"
    },
    17: {
        dexcode: "Sma21322"
    }
}

我希望从数组$dexcode_comp中返回所有未出现在$matches中的值。就像array_intersect函数的相反。

我该怎么做?我希望有人可以将我推向正确的方向。

2 个答案:

答案 0 :(得分:1)

这些是对象,而不是数组,因此您可以将json_encodejson_decode与第二个参数一起用作true,然后再创建关联数组,然后使用array_diff_assoc

$arr1 = json_decode(json_encode($obj1), true);
$arr2 = json_decode(json_encode($obj2), true);
$diff = array_diff_assoc($arr1, $arr2);

注意:如果您感兴趣的话,还有array_diff

阅读材料

json_encode

json_decode

array_diff_assoc

答案 1 :(得分:1)

您可以使用array-diff

$result = array_diff($matches, $dexcode_comp);

这将比较第一个数组和第二个数组,并返回一个数组,这些数组的值不相同。正如您所说的array_intersect

的相反