开发者

Remove duplicates from array

开发者 https://www.devze.com 2023-03-26 18:19 出处:网络
I have two arrays, like this: $array1 = array( \'ADAIR\', \'ADAM\', \'ADAMINA\', \'ADDISON\', \'ADDY\', \'ADELLE\',

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 firs开发者_StackOverflow社区t array and remove from it duplicates from second array.


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.


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);


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

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

codepad.org/NVkuml5g

0

精彩评论

暂无评论...
验证码 换一张
取 消