检查如果一个数组包含另一个数组的所有元素

编程入门 行业动态 更新时间:2024-10-26 00:22:35
本文介绍了检查如果一个数组包含另一个数组的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

检查如果一个数组包含另一个数组的所有元素

本文发布于:2023-10-27 11:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533261.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   元素

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!