Check if Two Arrays Have At Least One Common Element using PHP

array_intersect function

<?php

function containsAtLeastOneCommonElement(array $data1, array $data2): bool
{
    return !empty(array_intersect($data1, $data2));
}

$result = containsAtLeastOneCommonElement([1, 6, 9], [5, 9, 12]);
echo $result ? 'Yes' : 'No'; // Yes

$result = containsAtLeastOneCommonElement([2, 4, 8], [5, 9, 12]);
echo $result ? 'Yes' : 'No'; // No

Leave a Comment

Cancel reply

Your email address will not be published.