Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP检查一个数组是否包含另一个数组_Php_Arrays_Multidimensional Array_Array Intersect - Fatal编程技术网

使用PHP检查一个数组是否包含另一个数组

使用PHP检查一个数组是否包含另一个数组,php,arrays,multidimensional-array,array-intersect,Php,Arrays,Multidimensional Array,Array Intersect,可能重复: 我以前曾在Stackoverflow上发布过类似的内容,但答案并不完全让我满意。这就是为什么我再次发布这个问题,但一直在改变这个问题 一些人帮助我构造了一个函数,该函数检查多维数组($GroupOfEight[$i])中的元素数组($GroupOfEight)是否等于另一个数组($stackArray),而不考虑数组中的数字顺序 然而,我需要检查的是所提到的数组($stackArray)是否包含多维数组($GroupOfEight)[$I])中的任何其他数组($GroupOfEig

可能重复:

我以前曾在Stackoverflow上发布过类似的内容,但答案并不完全让我满意。这就是为什么我再次发布这个问题,但一直在改变这个问题

一些人帮助我构造了一个函数,该函数检查多维
数组($GroupOfEight[$i])
中的元素
数组($GroupOfEight)
是否等于另一个数组($stackArray),而不考虑数组中的数字顺序

然而,我需要检查的是所提到的
数组($stackArray)
是否包含多维
数组($GroupOfEight)[$I])
中的任何其他
数组($GroupOfEight)
,这意味着主
数组($stackArray)
可以包含比
子数组($GroupOfEight[$I])
更多的元素

以下是我迄今为止收集的一个工作代码,但需要修改为我想要的版本:

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


    $stackArray = array(0,4,12,1,9,8,5,13,9,2,5,2,10);
    /*$stackArray gets value with POST Method by URL parameter.
    This is just the example. As you see this array contains 
    $GroupOfEight[4], and also it contains many other numbers.*/


    /* The function given below checks if $stackArray equals any
    of the subarrays of $GroupOfEight. However, we want to check
    if $stackArray caontains any of the subarrays of function.
    If it does, function should return the index number, if it
    doesnt it should return -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;
}

// Calling the function that is given above.
echo searcheight($stackArray,$GroupOfEight);
?>

任何合乎逻辑的想法或解决方案都将不胜感激。谢谢。

这一款很快:

function contains_array($array){
    foreach($array as $value){
        if(is_array($value)) {
          return true;
        }
    }
    return false;
}
你可以试试

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

$stackArray = array(0,4,12,1,9,8,5,13,9,2,5,2,10);

function searcheight($stackArray, $GroupOfEight) {
    $list = array();
    for($i = 0; $i < count($GroupOfEight); $i ++) {
        $intercept = array_intersect($GroupOfEight[$i], $stackArray);
        $len = count($intercept);
        if ($len % 4 == 0) {
            $list[$i] = $len;
        }
    }
    arsort($list);
    if (empty($list))
        return - 1;
    return key($list);
}
echo searcheight($stackArray, $GroupOfEight);

您的预期结果是什么???对于假定的$stackArray,它应该给出4,这是$GroupOfEight4中包含的子数组索引号的索引是
数组(1,3,5,7,13,15,9,11)
$stackArray=array(0,4,12,1,9,8,5,13,9,2,10)。。你看不出名单上少了3个吗???哦,是的,但你知道了。它包含子阵列和其他元素。我得找到它里面有哪一个,是不是还没有。。是什么使array 4成为最有资格使用块括号{},即使对于像您这样的单行程序
返回true。这是一个好习惯,每当你跳转到使用预处理器和宏的语言中,你都可以使用FOR循环,如果你需要数组的位置,就返回计数器。或者,不是返回true,而是返回数组键$array。我不认为这一个完全符合我的要求,谢谢,尽管我同意@moz,但这是另一个问题的正确答案。我不知道为什么这里有这么多UV。爸爸,我已经做了修改函数,我已经转移到4组消除。请修改这个代码,告诉我哪里错了。函数searchFourTerms($groupOffFour有16mem){$len4=count($leftArray);$list4=array();for($i4=0;$i45