user2706762
user2706762

Reputation: 397

Check array if value exist in array. If yes delete it.

I have 2 arrays and I would like to find and delete the same values.

For example:

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

Final result should like:

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

Thanks.

Upvotes: 0

Views: 136

Answers (2)

chanchal118
chanchal118

Reputation: 3647

Use

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

Manual from array_merge() says

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.

In your case duplicate values are appended after array_merge, so you need to call array_unique after merging to remove duplicate values.

Upvotes: 6

GrahamTheDev
GrahamTheDev

Reputation: 24835

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

Hope that helps!

Upvotes: 5

Related Questions