Created
May 4, 2019 09:29
-
-
Save olotintemitope/550d2128ba3ea3728eafa1881f7a9e6d to your computer and use it in GitHub Desktop.
Find Array Intersection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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