检查如果一个数组包含另一个数组的所有元素 [英] Checking if an array contains all elements of another array

查看:341
本文介绍了检查如果一个数组包含另一个数组的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计的电气工程应用。不过,我被困在此:
我有以下阵列

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;
}
?>

请帮我纠正我的code或引进我这个问题的解决,
谢谢你。

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆