Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if an array contains all elements of another array

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

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

Please help me correct my code or introduce me the solution of this issue, Thanks.

EDIT: 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.

like image 257
mozcelikors Avatar asked Oct 12 '12 21:10

mozcelikors


2 Answers

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

EDIT: 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);
like image 56
Asad Saeeduddin Avatar answered Oct 05 '22 07:10

Asad Saeeduddin


You can 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]);
}
like image 26
Baba Avatar answered Oct 05 '22 06:10

Baba