Checking if an array contains all the elements of another array

I am developing an application for electrical engineering. However, I am stuck with 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 it is one size.

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

What I want to do is check if $ myStack is equal to any basement array of the $ GroupOfEight array. (Numerical ordering doesn't matter. The script should just check if all elements are contained. It doesn't matter if their order is the same or not.)

Here is what I have done to solve the problem 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; } ?> 

Please help me fix my code or introduce me a solution to this problem, Thank you.

EDIT: It should contain 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 N if there is an appropriate case.

+5
source share
6 answers

Given your array samples, the output of this will be:

 > 0 

If you have only one number pin, this should do the following:

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

EDIT: function created. Returns the first matched index or -1 for 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); 
+3
source

You may try:

 $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]); } 
+1
source

What is wrong with your initial approach is that you run a GroupOfEight loop twice . You have two for-loops.
First, you select each array in GroupOfEight, and in the second loop, you look at each value of the array.

If you want to use your original approach, get rid of the extra loop:

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

Demo: http://codepad.viper-7.com/0hRNHz

You can do the same with 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); } } 

Demo: http://codepad.viper-7.com/6uLd9L

Update
Associated SO record: Check if two array values ​​are equal (ignoring order)

+1
source

You are comparing count , which is not enough, as numbers can be changed. Try the following:

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

Count the total number of $ GroupOfEight [$ i]
Count the total amount of $ myStack
if the total values ​​are equal:
In the loop -
if $ myStack [$ c] - in_array ($ GroupOfEight [$ i]): $ equal = 1
else $ equal = 0; exit;

if $ equal == 1 β†’ β†’ the arrays are identical

0
source

We do not need loops. try it

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

Exit

0

Note: $key output is the location of the array in $ GroupOfEight ie ($ GroupOfEight [0])

0
source

Source: https://habr.com/ru/post/927951/


All Articles