从两个不同的数组中删除唯一条目

时间:2017-07-21 11:55:22

标签: php arrays

我有两个数组,想要代表id,title,sku删除重复的条目。我使用过array_diff函数,但是我没有得到所需的结果。

Array
(

[0] => Array
(
[id] => 7838585925
[title] => 1778-1978 CAPTAIN COOK DISCOVERS HAWAII - EXCLUSIVE 
ALBUM, GREAT BUY !!
[sku] => A-07582
)

[1] => Array
(
[id] => 7841101829
[title] => 1965 International Cooperation Year Crown Agents SET 62V MNH diff. countries
[sku] => A-02841
)

[2] => Array
(
[id] => 7840985093
[title] => 1977 - Silver Jubilee Coronation Queen Elizabeth II- 75V MNH, All PRISTINE!
[sku] => A-03025
)

[3] => Array
(
[id] => 7840685253
[title] => ADEN - 1937 KGVI CORONATION - 3V - FDC REGISTERED
[sku] => A-03548
)

[4] => Array
(
[id] => 7838687045
[title] => ADEN - 1937 KGVI CORONATION - 3V - FDC to EGYPT
[sku] => A-07374
)

Array ( 
[0] => Array 
(
[id] => 362016327490
[title] => ISRAEL - 1997 TRADITIONAL COSTUMES - 3V - MINT NH
[sku] => 
)

[1] => Array
(
[id] => 362016327492
[title] => KUWAIT - 1965 CENTRE SEPIA / BIRD - SG#286-293 -8V - MINT NH
[sku] => 
)

[2] => Array
(
[id] => 362016327493
[title] => UAE - 2012 SILVER JUBILEE OF SULTAN BIN ALI AL-OWAIS CULTURAL EST - FDC
[sku] => 
)

[3] => Array
(
[id] => 362016327494
[title] => UAE - 1996 NATIONAL DAY, 25th ANNIVERSARY SCOTT#547A - MIN/SHT - MINT NH ***
[sku] => 
)

[4] => Array
(
[id] => 362016327495
[title] => IRAQ - 1976 OLYMPIC GAMES, MONTREAL - MINATURE SHEET MINT NH
[sku] => 
)

[5] => Array
(
[id] => 362016327496
[title] => EGYPT - 1989 BICENTENARY OF FRENCH REVOLUTION - FDC
[sku] => 
)
)

PS-两个数组都包含数千个值 并使用PHP 请帮助解决问题,提前致谢

1 个答案:

答案 0 :(得分:0)

您可以这样做:

$result = [];
$cb = function ($arr) use (&$result) {
    in_array($arr, $result) || $result[] = $arr;
    return false;
};

array_map($cb, array_merge($a, $b));
print_r($result);

希望有所帮助

UPD例如源数据:

$a = [
    [
        'id' => '7838585925 ',
        'title' => '1778-1978 CAPTAIN COOK DISCOVERS HAWAII - EXCLUSIVE ALBUM, GREAT BUY !! ',
        'sku' => 'A-07582 ',
    ],
    [
        'id' => '7841101829',
        'title' => '1965 International Cooperation Year Crown Agents SET 62V MNH diff. countries',
        'sku' => 'A-02841',
    ],
];

$b = [
    [
        'id' => '7841101829',
        'title' => '1965 International Cooperation Year Crown Agents SET 62V MNH diff. countries',
        'sku' => 'A-02841',
    ],
    [
        'id' => '7840685253',
        'title' => 'ADEN - 1937 KGVI CORONATION - 3V - FDC REGISTERED',
        'sku' => 'A-03548',
    ],
];

输出:

Array
(
    [0] => Array
        (
            [id] => 7838585925 
            [title] => 1778-1978 CAPTAIN COOK DISCOVERS HAWAII - EXCLUSIVE ALBUM, GREAT BUY !! 
            [sku] => A-07582 
        )

    [1] => Array
        (
            [id] => 7841101829
            [title] => 1965 International Cooperation Year Crown Agents SET 62V MNH diff. countries
            [sku] => A-02841
        )

    [2] => Array
        (
            [id] => 7840685253
            [title] => ADEN - 1937 KGVI CORONATION - 3V - FDC REGISTERED
            [sku] => A-03548
        )

)