Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save olotintemitope/550d2128ba3ea3728eafa1881f7a9e6d to your computer and use it in GitHub Desktop.
Save olotintemitope/550d2128ba3ea3728eafa1881f7a9e6d to your computer and use it in GitHub Desktop.
Find Array Intersection
function findIntersection($array1, $array2) {
$intersect = [];
foreach($array2 as $value) {
if (array_key_exists($value, $array1) && $array1[$value] == $value) {
$intersect[] = $value;
}
}
return $intersect;
}
$array1 = ['1' => 1, '3' => 3, '2' => 2];
$array2 = [2,4,3];
print_r(findIntersection($array1, $array2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment