检查数组包含PHP另一个数组

检查数组包含PHP另一个数组

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

问题描述

可能重复:结果
  Checking如果一个数组包含另一个数组的所有元素

我已经发布类似这样的前#1,但答案并不完全满足我。这就是为什么我再次发布的问题,但始终改变的问题。

I have posted something like this to the Stackoverflow before, but the answers do not fully satisfy me. That's why I'm posting the question again, but changing the question all along.

有人帮助我建立一个检查功能,如果一个阵列($ GroupOfEight [$ i])是的元素多维阵列($ GroupOfEight)等于另一个数组($ stackArray),无论在阵列订货数量。

Some people helped me to construct a function that checks if an array($GroupOfEight[$i]) that is an element of a multidimensional array($GroupOfEight) equals another array($stackArray), disregarding the number ordering in the arrays.

不过,我需要检查的是提到阵列($ stackArray)是否包含任何其他阵列($ GroupOfEight [$ i]) 在多维阵列($ GroupOfEight)与否,这意味着主阵列($ stackArray)可包括比子数组多个元素($ GroupOfEight [$ i])

However, what I need to check is whether the mentioned array($stackArray) contains any another array($GroupOfEight[$i]) in the multidimensional array($GroupOfEight) or not, that means main array($stackArray) can consist more elements than subarrays($GroupOfEight[$i]).

下面是一个工作code是我到目前为止云集,但需要进行修改,以我想要的版本:

Here is the one working code that I've gathered so far, but need to be modified to the version I want:

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

任何合乎逻辑的想法或解决方案将亲切太大AP preciated。谢谢你。

Any logical ideas or solutions will kindly be much appreciated. Thanks.

推荐答案

您可以尝试

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

输出

5

这篇关于检查数组包含PHP另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 18:07