检查一个数组是否包含另一个数组的所有元素 [英] Checking if an array contains all elements of another array
问题描述
我正在设计一个电气工程应用程序.但是,我坚持这一点:我有以下数组
I'm designing an electrical engineering application. However, i'm stuck on this: I have the following array
<?php
// Static Array
$GroupOfEight = array (
array(0,1,3,2,4,5,7,6),
array(4,5,6,7,16,12,13,14),
array(12,13,15,14,8,9,11,10),
array(2,6,14,10,3,7,15,11),
array(1,3,5,7,13,15,9,11),
array(0,4,12,8,1,5,13,9),
array(0,1,3,2,8,9,11,10)
);
?>
我还有另一个数组,但这个数组是一维的.
And I have another array, but this one is one dimensional.
<?php
$myStack = array(0,1,3,2,4,5,7,6); //Dynamic, gets value by POST method.
?>
我想要做的是检查 $myStack 是否等于 $GroupOfEight 数组的任何子数组.(数字排序并不重要.脚本应该只检查是否包含每个元素.它们的顺序是否相同并不重要.)
What I want to do is to check if $myStack is equal to any sub array of $GroupOfEight array. ( Number ordering is not important. The script should just check if every elements contained. It's not important if their order is same or not. )
这是我目前为解决这个问题所做的工作:
Here is what I've done to solve the issue so far:
<?php
//Check if stackArray contains 8group
for($i=0; $i<count($GroupOfEight);$i++)
for($j=0; $j<count($GroupOfEight[$i]); $j++){
//$containsSearch = count(array_intersect($search_this,$all)) == count($search_this);
$containsSearch = count(array_intersect($stackArray,$GroupOfEight[$j])) == count($stackArray);
echo $containsSearch;
}
?>
请帮我更正我的代码或向我介绍这个问题的解决方案,谢谢.
Please help me correct my code or introduce me the solution of this issue, Thanks.
它应该只给出 1 个索引号.例如 stackArray 是 0,1,3,2,4,1,2,3 并且它应该找到匹配相同数字的 GroupOfEight[N],而不管数字的顺序如何.如果有匹配的情况,我应该得到 N.
It should give only 1 index number. for example stackArray is 0,1,3,2,4,1,2,3 and it should find GroupOfEight[N] that matches the same numbers, regardless of the order of the numbers. I should get the N if there is a matching case.
推荐答案
给定您的示例数组,输出将是:
Given your sample arrays, the output of this will be:
> 0
如果您必须只有一个数字输出,则应该这样做:
In case you HAD to have only one number output, this should do that:
<?php
//Check if stackArray contains 8group
$check=false;
for($i=0; $i<count($GroupOfEight);$i++){
//$containsSearch = count(array_intersect($search_this,$all)) == count($search_this);
$containsSearch = (count(array_intersect($stackArray,$GroupOfEight[$i])) == count($stackArray) && count(array_intersect($stackArray,$GroupOfEight[$i])) == count($GroupOfEight[$i]));
if($containsSearch && !$check){
echo $i; //This specifies which index in GroupOfEight contains a matching array
$check=true;
}
}
?>
做了一个功能.返回第一个匹配的索引或 -1 没有匹配:
Made a function. Returns first matched index or -1 for no matches:
function searcheight($stackArray,$GroupOfEight){
for($i=0; $i<count($GroupOfEight);$i++){
$containsSearch = (count(array_intersect($stackArray,$GroupOfEight[$i])) == count($stackArray) && count(array_intersect($stackArray,$GroupOfEight[$i])) == count($GroupOfEight[$i]));
if($containsSearch){
return $i; //This specifies which index in GroupOfEight contains a matching array
}
}
return -1;
}
echo searcheight($stackArray,$GroupOfEight);
这篇关于检查一个数组是否包含另一个数组的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!