检查数组是否包含另一个数组的所有元素

时间:2012-10-12 21:25:02

标签: php arrays multidimensional-array array-intersect

我正在设计电气工程应用。但是,我坚持这个: 我有以下数组

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

我有另一个阵列,但这个是一维的。

<?php
$myStack = array(0,1,3,2,4,5,7,6); //Dynamic, gets value by POST method.
?>

我想要做的是检查$ myStack是否等于$ GroupOfEight数组的任何子数组。 (数字排序并不重要。脚本应该只检查每个元素是否包含。如果它们的顺序相同,它就不重要。)

以下是我目前为解决此问题所做的工作:

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

请帮我纠正我的代码或向我介绍此问题的解决方案, 感谢。

编辑:它应该只提供1个索引号。例如,stackArray是0,1,3,2,4,1,2,3,它应该找到匹配相同数字的GroupOfEight [N],而不管数字的顺序如何。如果有匹配的情况,我应该得到N.

6 个答案:

答案 0 :(得分:3)

给定样本数组,输出结果为:

> 0

如果你只有一个数字输出,这应该这样做:

<?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表示没有匹配:

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);

答案 1 :(得分:1)

您可以尝试:

$searchKeys = array();
foreach ( $GroupOfEight as $key => $values ) {
    (count(array_intersect($values, $myStack)) == count($myStack)) and $searchKeys[] = $key;
}

#Output all keys it found same match
var_dump($searchKeys);

#OR Output Each Array it found a match
foreach($searchKeys as $key)
{
    var_dump($GroupOfEight[$key]);
}

答案 2 :(得分:1)

您的原始方法出现问题的方法是循环使用GroupOfEight 两次。你有两个for循环。
首先,选择GroupOfEight中的每个数组,然后在第二个for循环中选择数组的每个值。

如果您想使用原始方法,请摆脱额外的for循环:

echo "Hello, World!";
$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)
              );

$myStack = array(0,1,3,2,4,5,7,6); //Dynamic, gets value by POST method.


for($i=0; $i<count($GroupOfEight);$i++) {       
    $containsSearch = count(array_intersect($myStack,$GroupOfEight[$i])) == count($myStack);
    if($containsSearch===true) {
        echo "Woo! GroupOfEight[$i], <br/>" . print_r($GroupOfEight[$i], true) . "<br/>==<br/>" . print_r($myStack, true);
    }
}

演示:http://codepad.viper-7.com/0hRNHz

您可以使用array_diff完成相同的操作:

for($i=0; $i<count($GroupOfEight);$i++) {               
    if(count(array_diff($myStack,$GroupOfEight[$i]))==0) {
        echo "Woo! GroupOfEight[$i], <br/>" . print_r($GroupOfEight[$i], true) . "<br/>==<br/>" . print_r($myStack, true);
    }
}

演示:http://codepad.viper-7.com/6uLd9L

<强>更新
相关的SO帖子是:Check whether two array values are equal (ignoring order)

答案 3 :(得分:0)

您正在比较count这是不够的,因为数字可以更改。试试这个:

// 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)
    );

$myStack = array(0,1,3,2,4,5,7,6); //Dynamic, gets value by POST method.

$containsSearch = false;
foreach($GroupOfEight as $key => $value){
    if ($myStack == $value) {
        $containsSearch = true;
    }

}

var_dump($containsSearch);

答案 4 :(得分:0)

计算$ GroupOfEight [$ i]的总数 计算$ myStack总数 如果总数相等:
循环中 -
如果$ myStack [$ c]是in_array($ GroupOfEight [$ i]):$ equal = 1
else $ equal = 0;出口;

如果$ equal == 1 - &gt;数组是相同的

答案 5 :(得分:0)

我们不需要任何循环。试试这个

<?php 
$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)
                      );
$myStack = array(0,1,3,2,4,5,7,6);

$key = '';
$key = array_search($myStack,$GroupOfEight);
echo $key;
?>

输出

  

0

注意:输出$key是$ GroupOfEight中数组的位置,即($ GroupOfEight [0])