dqdes60666 2012-08-01 19:58
浏览 21
已采纳

检查两个PHP列表是否完全不相交

In PHP, one can use the following function to determine if one list (the child) is a subset of another (the parent):

function issubset($child, $parent)
{
        $c = count($child);
        $valid = 1;
        for($i=0;$i<$c;$i++) {
            if(!in_array($child[$i], $parent)) {
                $valid = 0;
                return $valid;
            }
        }
    return $valid;
}

A similar but opposite concept is the idea of two lists being disjoint, whereby they have no elements in common whatsoever.

For example, the lists 1,2,3,4 and 4,5,6,7 are not disjoint because they have the common element 4, but the lists 1,2,3 and 4,5,6 are disjoint as they have no elements in common.

How might a function to check disjointness be designed?

  • 写回答

2条回答 默认 最新

  • douliang4858 2012-08-01 20:01
    关注

    if (count(array_intersect($a, $b)) == 0) { /* do something */ }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?