一尘不染

PHP-将两个数组合并为一个数组(也删除重复项)

php

嗨,我正在尝试合并两个数组,并且还想从最终数组中删除重复的值。

这是我的数组1:

Array
    (
    [0] => stdClass Object
    (
    [ID] => 749
    [post_author] => 1
    [post_date] => 2012-11-20 06:26:07
    [post_date_gmt] => 2012-11-20 06:26:07
)

这是我的数组2:

Array
(
[0] => stdClass Object
(
[ID] => 749
[post_author] => 1
[post_date] => 2012-11-20 06:26:07
[post_date_gmt] => 2012-11-20 06:26:07

)

array_merge用于将两个数组合并为一个数组。它正在给出这样的输出

Array
(
[0] => stdClass Object
(
[ID] => 749
[post_author] => 1
[post_date] => 2012-11-20 06:26:07
[post_date_gmt] => 2012-11-20 06:26:07

[1] => stdClass Object
(
[ID] => 749
[post_author] => 1
[post_date] => 2012-11-20 06:26:07
[post_date_gmt] => 2012-11-20 06:26:07

)

我要删除这些重复的条目,或者在合并之前删除它们…请帮助。.谢谢!!!


阅读 622

收藏
2020-05-29

共1个答案

一尘不染

array_unique(array_merge($array1,$array2), SORT_REGULAR);

http://se2.php.net/manual/zh/function.array-
unique.php

2020-05-29