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

use*_*762 0 php

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

例如:

$array_1=array('a','b','c');    
$array_2=array('3','43','b');  
Run Code Online (Sandbox Code Playgroud)

最终结果应该是:

$final_array=('a','b','c','3','43');
Run Code Online (Sandbox Code Playgroud)

谢谢.

cha*_*118 6

使用

$final_array = array_unique(array_merge($array_1, $array_2));
Run Code Online (Sandbox Code Playgroud)

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在合并后调用以删除重复值.


Gra*_*hie 5

$final_array = array_unique( array_merge($array_1, $array_2) );
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!