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>string5>string6>";
$arr1 = explode(">", $s1);
$s2 = "string1>string4>string5";
$arr2 = explode(">", $s2);
$isSubset = /* ??? */
6 个解决方案
#1
15
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.
如果从字符串开始,可以检查strstr($ fullString,$ subsetStr);.但这只会在所有字符具有相同顺序时起作用:'abcd','cd'可以工作,但'abcd','ad'不会。
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
:
但是你不应该编写自己的自定义函数,而应该知道PHP有大量的数组函数,因此不可能没有std函数可以做你需要它做的事情。在这种情况下,我建议使用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;
}
#2
41
if (array_intersect($array1, $array2) == $array1) {
// $array1 is a subset of $array2
}
#3
7
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
参考:array_diff
You can use array_intersect
also.
您也可以使用array_intersect。
#4
1
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.
我将创建一个较大数组的关联数组,然后遍历较小的数组,寻找非碰撞,如果找到一个,则返回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;
#5
1
$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;
}
#6
0
Simple function which will return true if array is exact subset otherwise false. Solution is applicable for two dimensional array as well.
简单函数,如果数组是精确子集,则返回true,否则返回false。解决方案也适用于二维阵列。
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;
}
#1
15
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.
如果从字符串开始,可以检查strstr($ fullString,$ subsetStr);.但这只会在所有字符具有相同顺序时起作用:'abcd','cd'可以工作,但'abcd','ad'不会。
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
:
但是你不应该编写自己的自定义函数,而应该知道PHP有大量的数组函数,因此不可能没有std函数可以做你需要它做的事情。在这种情况下,我建议使用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;
}
#2
41
if (array_intersect($array1, $array2) == $array1) {
// $array1 is a subset of $array2
}
#3
7
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
参考:array_diff
You can use array_intersect
also.
您也可以使用array_intersect。
#4
1
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.
我将创建一个较大数组的关联数组,然后遍历较小的数组,寻找非碰撞,如果找到一个,则返回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;
#5
1
$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;
}
#6
0
Simple function which will return true if array is exact subset otherwise false. Solution is applicable for two dimensional array as well.
简单函数,如果数组是精确子集,则返回true,否则返回false。解决方案也适用于二维阵列。
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;
}