Test if one array is a subset of another

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

提交回复
热议问题