3

I have two arrays, like this:

$array1 = array(
'ADAIR',
'ADAM',
'ADAMINA',
'ADDISON',
'ADDY',
'ADELLE',
'ADEN',
'ADOLPH',
'ADRIANNA'
);

$array2 = array(
'ADAIR',
'ADAMINA',
'ADRIANNA'
);

How do I make a third array, without duplicates? We should take first array and remove from it duplicates from second array.

1
  • This question is a bit ambiguous. Do you want ADAIR, ADAMINA and ADRIANNA to be in the third array or not? Commented Aug 5, 2011 at 14:39

3 Answers 3

6

Use Array-diff

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

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

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

1 Comment

+1 I think this is what the OP meant, though he could clarify a bit
2

Take a look here: http://php.net/manual/en/function.array-unique.php

Combine both arrays into 1, then run them through the array-unique function

$result = array_unique($combined);

2 Comments

@RiaD I think the question is a bit ambiguous; it asks "How do I make a third array, without duplicates?". Well, this answer is perfectly valid then.
@RiaD Yeah, I'm still confused by what he actually wants then. . . The bold part was added later, I answered the question as it was asked originally, I think. . .
0

@grunk deleted a perferctly valid answer, so credits not to me:

$unique = array_unique(array_merge($array1,$array2));

codepad.org/NVkuml5g

1 Comment

You'll get a merged version of $array1 and $array2, which doesn't seem to be what the OP wanted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.