6

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.

mozcelikors
  • 2,582
  • 8
  • 43
  • 77

6 Answers6

3

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);
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • seems like the error has disappeared, but let me ask you something. How do i check the $stackArray for each indexes in $GroupOfEight, and return the index number if there is any. That will totally fix my problem and will save me some time. Thanks. – mozcelikors Oct 12 '12 at 21:37
  • when you run array_intersect, that returns an array filled with the values present in both. – Asad Saeeduddin Oct 12 '12 at 21:41
  • I have edited it to also echo the number. So if you get true0true1false2true3... that means stackArray matches 0,1,3 etc. Obviously you want to do something better than just echo the index. – Asad Saeeduddin Oct 12 '12 at 21:47
  • Its not what I ask actually, 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. Please help. Thanks – mozcelikors Oct 12 '12 at 21:51
  • Well if you are certain only one member of GroupOfEight matches, this will output the index of that one. – Asad Saeeduddin Oct 12 '12 at 21:55
  • `$i` is the 'N' you are looking for. This answer would work just fine for you. – Terry Seidler Oct 12 '12 at 21:57
  • It would seem he requires the output to be typed as a single integer, regardless of the fact that several matches are possible. I will modify to return only the first index. – Asad Saeeduddin Oct 12 '12 at 21:59
  • This seems working, only problem not is: I may get 0 index number also. So i when i give any values to $stackArray that does not satisfy the program outputs 0. So how to make 0 distinct from the false case. – mozcelikors Oct 12 '12 at 22:07
  • actually, it should output nothing if you give it a false case. let me test it. – Asad Saeeduddin Oct 12 '12 at 22:09
  • 1
    Yeah there was a problem, I had to check intersections both ways. Fixed now, here is the [fiddle](http://www.phpfiddle.org/main/code/mf3-cg0) – Asad Saeeduddin Oct 12 '12 at 22:18
  • It is the best answer anyways, If you can figure that false case out, please post here. I'm new at array algorithms and function, thats why i need to practice about it. I appreciate all your help. – mozcelikors Oct 12 '12 at 22:18
  • Let me ask you one more thing, Asad. I want to extend $stackArray. How to convert to code to this case: When we give extra elements in stackArray it should still select the most matched group and give the index number. Excuse me if im not clear. – mozcelikors Oct 12 '12 at 22:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17959/discussion-between-asad-and-mozcelikors) – Asad Saeeduddin Oct 12 '12 at 22:40
1

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]);
}
Baba
  • 94,024
  • 28
  • 166
  • 217
  • (count(array_intersect($values, $myStack)) == count($myStack)) and $searchKeys[] = $key; This line encounters a problem – mozcelikors Oct 12 '12 at 21:44
  • It works fine ... http://codepad.viper-7.com/M718f0 also see http://codepad.org/JUZdy1zF .. what verion of PHP are you using ??? – Baba Oct 12 '12 at 21:45
  • Warning: array_intersect() [function.array-intersect]: Argument #2 is not an array in \scripts\script.php on line 1771 array(0) { } This is the error i got – mozcelikors Oct 12 '12 at 21:47
  • Please note that i used `$myStack` ???? You are having both $stackArray and $myStack ... am sure that is where thing are missed up – Baba Oct 12 '12 at 21:48
  • Yes I mistyped myStack, i see. but this is what i seek :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 only 1 N number if there is a matching case. Please help. Thanks – mozcelikors Oct 12 '12 at 21:54
  • @mozcelikors but `0,1,3,2,4,1,2,3` is not in the array list above .. why should it show `1` ??? If i understand your logic am sore i can come up with something simple – Baba Oct 12 '12 at 21:57
  • I gave 0,1,3,2.. as an example.. Let me restate it: Think of $stackArray as 0,1,3,2,4,5,7,6. That is $GroupOfEight[0] as well. I should get the "0" number (that is index number) in this case. But if the ordering is shuffled like this: $stackArray = 0,1,2,3,4,12,3. (Which the $GroupOfEight does not contain) then a single echo "not satisfied"; output is enough. I hope its clear. – mozcelikors Oct 12 '12 at 22:02
  • @mozcelikors there is no place where `12` and `3` are in the same array ... so how is `0,1,2,3,4,12,3` in `$GroupOfEight` ?? am not also using `echo` am adding all keys to `$searchKeys` for you – Baba Oct 12 '12 at 22:07
1

The thing that is wrong with your original approach is that you loop through GroupOfEight twice. You have two for-loops.
First you select every array within GroupOfEight and in the second for loop you go through each value of the array.

If you would like to use your original approach, get rid of the extra for 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 could accomplish 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
A related SO post is: Check whether two array values are equal (ignoring order)

Community
  • 1
  • 1
Terry Seidler
  • 2,043
  • 15
  • 28
0

You are comparing count which is not enough, as numbers could be changed. Try this:

// 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);
Riz
  • 9,703
  • 8
  • 38
  • 54
0

Count the total of $GroupOfEight[$i]
Count the total of $myStack
if totals are equal:
In a loop -
if $myStack[$c] is in_array($GroupOfEight[$i]): $equal = 1
else $equal = 0; exit;

if $equal == 1 -> arrays are identical

rinchik
  • 2,642
  • 8
  • 29
  • 46
  • please format your code better and use Code Sample around your code snippets. –  Oct 12 '12 at 21:36
0

We do not need any loops. Try this

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

Output

0

Note: Output $key is location of the array in $GroupOfEight i.e ( $GroupOfEight[0] )

hemnath mouli
  • 2,617
  • 2
  • 17
  • 35