This will give you the desired result:
$array1 = array(1, 2, 2, 3); $array2 = array( 1, 2, 3,4); $countArray1 = array_count_values($array1); $countArray2 = array_count_values($array2); foreach($countArray1 as $value=>$count) { if($count > 1) $dupArray[] = $value; } foreach($countArray2 as $value=>$count) { if($count > 1) $dupArray[] = $value; } print_r($dupArray);
Result
Array
(
[0] => 2
) Explanation
Using array_count_values will read all array values ββthat look like this:
Array
(
[1] => 1
[2] => 2
[3] => 1
)
Array
(
[1] => 1
[2] => 1
[3] => 1
[4] => 1
) Then we iterate over each array_count_values to find values ββthat happen more than once. This will work if you have multiple sets of duplicate values:
$array1 = array(1, 2, 2, 3); $array2 = array( 1, 2, 3, 4, 3);
Result
Array
(
[0] => 2
[1] => 3
) source share