-2

I have a very simple PHP question.

Imagine i have two array :

[array1] {
[0] => zero
[1] => one
[2] => two
[3] => three 
}

and

[array2] {
[0] => zero
[1] => test1
[2] => test2
[3] => three 
}

I want to delete every value from the second array that is in the first one.

For example, from that two arrays at top, I want to have this below array ::

[array2] {
[0] => test1
[1] => test2
}

How can we do it in PHP ?

Thanks in advance.

3
  • @showdev this is Not duplicate ! just read quesion with examples again u will under stand. Commented Aug 1, 2014 at 21:13
  • 2
    They are both duplicates. All of these answer your question. Please explain how they are unsatisfactory or edit your question to help clarify. Commented Aug 1, 2014 at 21:14
  • @showdev You are right, i am sorry :X Commented Aug 2, 2014 at 8:34

1 Answer 1

5

You can use array_diff():

$array2 = array_diff($array2, $array1);

Edit: Here is an example:

$array1 = array('zero', 'one', 'two', 'three');
$array2 = array('zero', 'test1', 'test2', 'three');

$array2 = array_diff($array2, $array1);
print_r($array2);
Sign up to request clarification or add additional context in comments.

4 Comments

This is not the thing i need ! as @JaredFarrish showed !
@Cab I tested the code before I posted it. Added the test code to my answer. Can you please explain what is not working as expected?
@Cab - It does exactly what you describe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.