Tommy Lee Tommy Lee - 1 month ago 6
PHP Question

PHP comparing array elements as a whole

For example I have two arrays,

$arrayA = [0=>"A", 1=>"B", 2=>"C", 3=>"C", 4=>"E", 5=>"F"];
$arrayB = [0=>"B", 1=>"C"];


If I use array_intersect():

$same = array_intersect($arrayA, $arrayB);


I will get the result of
$same


1 => B
2 => C
3 => C


Instead, what I want to achieve is to get only

1 => B
2 => C

Answer

array_intersect(array_unique($arrayA),array_unique($arrayB))
Hope this can help.

array_unique doc