0

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.

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was almost about to post this :-) you guys are rock!
5
$final_array = array_unique( array_merge($array_1, $array_2) );

Hope that helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.