Testing for no array intersection in PHP -
i have following code:
$result = array_intersect($contacts1, $contacts2);
this generates:
array ( [21] => [22] => [23] => [24] => [25] => [26] => [28] =>
i have following if statement:
if (empty($result)) { // i.e. no interection
i realized not work test no intersection because many elements produced, value null. given best way test intersection of 2 arrays?
you can check if values null (if know no null's can exist in arrays). can use array_filter function.
for example:
$result = array_filter(array_intersect($contacts1, $contacts2));
that way, nulls removed, , result (if no intersecion exists) empty array.
update: said in comment, remove non-null values. revised version use callback function:
function filteronlynulls($elem) { return $elem !== null; } $result = array_filter(array_intersect($contacts1, $contacts2), "filteronlynulls");
Comments
Post a Comment