如果数组中存在值,则检查数组。如果是,请删除它。

时间:2014-01-05 10:20:05

标签: php

我有2个数组,我想找到并删除相同的值。

例如:

$array_1=array('a','b','c');    
$array_2=array('3','43','b');  

最终结果应该是:

$final_array=('a','b','c','3','43');

感谢。

2 个答案:

答案 0 :(得分:6)

使用

$final_array = array_unique(array_merge($array_1, $array_2));

来自array_merge()的手册

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

在您的情况下,重复值会在array_merge之后追加,因此您需要在合并后调用array_unique以删除重复值。

答案 1 :(得分:5)

$final_array = array_unique( array_merge($array_1, $array_2) );

希望有所帮助!