Test if one array is a subset of another

前端 未结 6 1293
庸人自扰
庸人自扰 2021-02-13 05:59

How can I determine if one array is a subset of another (all elements in the first are present in the second)?

 $s1 = \"string1>string2>string3>string4&         


        
相关标签:
6条回答
  • 2021-02-13 06:40

    Simple: use array subtraction.

    On array subtraction, you will know whether or not one array is a subset of the other.

    Example:

    if (!array_diff($array1, $array2)) {
        // $array1 is a subset of $array2
    }
    

    Reference: array_diff

    You can use array_intersect also.

    0 讨论(0)
  • 2021-02-13 06:54

    If you start from strings, you could check strstr($fullString,$subsetStr);. But that'll only work when all chars have the same order: 'abcd','cd' will work, but 'abcd','ad' won't.

    But instead of writing your own, custom, function you should know that PHP has TONS of array functions, so its neigh on impossible that there isn't a std function that can do what you need it to do. In this case, I'd suggest array_diff:

    $srcString = explode('>','string1>string2>string3>string4>string5');
    $subset = explode('>','string3>string2>string5');
    $isSubset = array_diff($subset,$srcString);
    //if (empty($isSubset)) --> cf comments: somewhat safer branch:
    if (!$isSubset)
    {
        echo 'Subset';
        return true;
    }
    else
    {
        echo 'Nope, substrings: '.implode(', ',$isSubset).' Didn\'t match';
        return false;
    }
    
    0 讨论(0)
  • 2021-02-13 06:54
    if (array_intersect($array1, $array2) == $array1) {
        // $array1 is a subset of $array2
    }
    
    0 讨论(0)
  • 2021-02-13 06:54

    I would create an associated array of the larger array, then iterate through the smaller array, looking for a non collision, if you find one, return false.

    function isSubset($arr1,$arr2){
        $map = Array();
        for ($i=0;$i<count($arr1);$i++){
          $map[$arr[$i]]=true;
        }
        for ($i=0;$i<count($arr2);$i++){
           if (!isset($map[$arr2[$i]])){
              return false;
           }
        }
        return true;
    
    0 讨论(0)
  • 2021-02-13 06:56
    $s1 = "1>2>3>4>5>6>7";
    
    $arr1 = explode(">",$s1);
    
    $s2 = "1>2>3";
    
    $arr2 = explode(">",$s2); 
    
    if(isSub($arr1,$arr2)){
    
             echo 'true';
    
    }else{
    
             echo 'false';
    }
    
    function isSub($a1,$a2){
    
        $num2 = count($a2);
        $sub  = $num2;
    
        for($i = 0;$i < $num2 ;$i++){
            if(in_array($a2[$i],$a1)){
                $sub--;
            }
        }
        return ($sub==0)? true:false;
    }
    
    0 讨论(0)
  • 2021-02-13 07:01

    Simple function which will return true if array is exact subset otherwise false. Solution is applicable for two dimensional array as well.

     function is_array_subset($superArr, $subArr) {
            foreach ($subArr as $key => $value) {
                //check if keys not set in super array OR values are unequal in both array.
                if (!isset($superArr[$key]) || $superArr[$key] != $value) {
                    return false;
                }
            }
            return true;
        }
    
    0 讨论(0)
提交回复
热议问题