8

Possible Duplicate:
Checking if an array contains all elements of another array

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.

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.

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]).

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

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

Community
  • 1
  • 1
mozcelikors
  • 2,582
  • 8
  • 43
  • 77
  • What is your expected result ??? – Baba Oct 17 '12 at 14:51
  • For the assumed $stackArray, it should give 4, which is the index of the contained subarray index number in $GroupOfEight – mozcelikors Oct 17 '12 at 14:57
  • 4 is `array(1,3,5,7,13,15,9,11)` and `$stackArray = array(0,4,12,1,9,8,5,13,9,2,5,2,10);` .. can't you see 3 is missing in the list ??? – Baba Oct 17 '12 at 14:59
  • oh yes, but you got the idea. it contains a subarray and other elements. i gotta find which one it contains, or does it not. – mozcelikors Oct 17 '12 at 15:00
  • Not quite yet .. what makes array 4 the most qualified – Baba Oct 17 '12 at 15:01
  • Even `array(0,4,12,8,1,5,13,9)` has more numbers in the search than number 4 – Baba Oct 17 '12 at 15:02
  • it does not matter. when the page loads, user will enter some numbers. and first I will check if there is any contained 8 groups. if it does not i will check for 4 groups. and I will eliminate them. But I ask you about this function only. This will be very time saving for me. – mozcelikors Oct 17 '12 at 15:02
  • so the `$stackArray` length has nothing to do with the result .. you are just checking in possibility of `16` `8` `4` etc ??? – Baba Oct 17 '12 at 15:07
  • yes, stackArray length is maximum 16, does not matter if less. If less than 8, i will move to 4 group elimination, but that i can take care of, i guess. – mozcelikors Oct 17 '12 at 15:09
  • I get you now .... 100% .. but you are also wrong .. the right answer is 5 .. because 4 only contains 4 possibly while 5 contains all the 8 .. count count it your self ...:) – Baba Oct 17 '12 at 15:13
  • yes, my mind is blown. sorry about that:) its good finally some one gets exactly what i ask for. – mozcelikors Oct 17 '12 at 15:14
  • lol .. hold on am writing the script for you – Baba Oct 17 '12 at 15:16
  • thanks buddy, i've given it hours, but still nothing. i gotta do more practice about arrays. – mozcelikors Oct 17 '12 at 15:17
  • You're the man! I can use this function for the four groups also i guess. Thanks. Very smooth answer. Understandable script. Great work. – mozcelikors Oct 17 '12 at 15:24
  • where does that %4 term comes from anyway. I'm just wondering. – mozcelikors Oct 17 '12 at 15:26
  • 16 , 8 , 4 .. they are all dividable by 4 ... its modules .. I figured i should just look for all values in the other array that is dividable by 4 – Baba Oct 17 '12 at 15:27
  • great, now, one more thing. If they are more than 4 groups, what should i do. Use a for loop with this function? or this function can give multiple indexes? it seems like it can give multiple index' – mozcelikors Oct 17 '12 at 15:29
  • What do you mean by if they are more than 4 groups – Baba Oct 17 '12 at 15:40
  • In the 4group elimination, i.e. $stackArray contains $GroupOfFour[5] and $GroupOfFour[2], does this function work? – mozcelikors Oct 17 '12 at 15:41
  • It should work so far its dividable by 4 .. we would capture it – Baba Oct 17 '12 at 15:44
  • what if i make $GroupOfTwo and and change that 4 into 2, that probably changes the results, doesnt it ? – mozcelikors Oct 17 '12 at 15:45
  • yes it does .. it looks for all intersection dividable by 2 instead – Baba Oct 17 '12 at 15:49

2 Answers2

9

This one is fast:

function contains_array($array){
    foreach($array as $value){
        if(is_array($value)) {
          return true;
        }
    }
    return false;
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Adder
  • 5,708
  • 1
  • 28
  • 56
  • 1
    always use block brackets {}, even for oneliners like yours `return true;`. It's good habit that pays of whenever you jump into languages using preprocessor and macros... – Marcin Orlowski Oct 17 '12 at 14:54
  • You can use a FOR loop, and return the counter if you need the position of the array. Or, instead of returning true, return the array key $array. – Shawn Wernig Oct 17 '12 at 14:57
  • 2
    I dont think this one does exactly what i asked for, thanks though – mozcelikors Oct 17 '12 at 15:00
  • 1
    I agree with @moz This is the correct answer to a different question. I have no idea why this has so many UVs. – mickmackusa Oct 11 '20 at 06:55
3

You can try

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

Output

5
Baba
  • 94,024
  • 28
  • 166
  • 217
  • Baba, I've made modification functions and I've moved to 4 group elimination. Please revise this code and tell where i am wrong. ($GroupOfFour has 16mem) function searchFourTerms($leftArray, $GroupOfFour) { $len4 = count($leftArray); $list4 = array(); for($i4 = 0; $i4 < count($GroupOfFour); $i4 ++) { $intercept4 = array_intersect($GroupOfFour[$i4], $leftArray); $len4 = count($intercept4); if (count($intercept4) % 4 == 0) { $list4[$i4] = $len4; } } arsort($list4); if (empty($list4)) return - 1; return key($list4); } – mozcelikors Oct 21 '12 at 10:43
  • I think I solved it, but still got some issues. Please move this to chat when you're here. – mozcelikors Oct 21 '12 at 11:06
  • 1
    This answer is unexplained and the other answer (with 12 UVs) is 100% wrong. This page is thus low-value to researchers. – mickmackusa Oct 11 '20 at 06:56