嗨,我正在尝试合并两个数组,并且还想从最终数组中删除重复值。
这是我的阵列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
)
我想删除这些重复的条目,或者我可以在合并之前将其删除...请帮助..谢谢!!!!!!!
array_unique(array_merge($array1,$array2), SORT_REGULAR);
如前所述,可以使用array_unique(),但仅限于处理简单数据时。对象不是那么容易处理。
当php尝试合并数组时,它会尝试比较数组成员的值。如果成员是对象,则它无法获取其值并使用spl散列。 Read more about spl_object_hash here.
简单地说,如果你有两个对象,同一个类的实例,如果其中一个不是对另一个的引用 - 你将最终拥有两个对象,无论它们的属性值如何。
为了确保你在合并数组中没有任何重复项,Imho你应该自己处理这个案例。
此外,如果要合并多维数组,请考虑在array_merge_recursive()上使用array_merge()。
它将合并两个数组并删除重复
<?php
$first = 'your first array';
$second = 'your second array';
$result = array_merge($first,$second);
print_r($result);
$result1= array_unique($result);
print_r($result1);
?>
试试这个链接link1
尝试使用array_unique()
这消除了数组列表中的重复数据..
合并两个数组不会删除副本,您可以尝试下面的示例从两个数组中获取唯一
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
print_r($result);