Test if one array is a subset of another

前端 未结 6 1312
庸人自扰
庸人自扰 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: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;
    }
    

提交回复
热议问题