Built in support for sets in PHP?

后端 未结 5 1029
一生所求
一生所求 2020-12-05 16:45

I\'m looking for a simple way to create an array in php that will not allow duplicate entries, but allows for easy combining of other sets or arrays.

I\'m mostly int

5条回答
  •  情深已故
    2020-12-05 17:30

    The answer is no, there is not a native set solution inside PHP. There is a Set data structure, but that is not baseline PHP.

    There is a convention for implementing sets using maps (i.e. associative arrays) in any language. And for PHP you should use true as the bottom value.

    true, 5=>true, 7=>true];
    $right = [6=>true, 7=>true, 8=>true, 9=>true];
    
    $union = $left + $right;
    $intersection = array_intersect_assoc($left, $right);
    
    var_dump($left, $right, $union, $intersection);
    

提交回复
热议问题