0

This is the third time that I'm posting about this topic. User Baba helped me everytime. Since the SO website has not a user message system -unfortunately- I'm asking a question within a post. Again.

With the help of the user Baba, we have managed to create a function that checks whether an array contains another array, but the function determines it multiple times and creates an index list. What the function lacks and I need to know is that the function does not return -1 in the exceptions like expected. Function may return values that are not related, and the function is not consistent. We simply need to check which arrays are matched with the main array($leftArray) and return the index's of those arrays. If there is not any matched array, the function have to return -1.

Please review this code and help me:

<?php
$leftArray = array(7,6,14,15,8,0,1,4,5,9); 
//contains $GroupOfFour[6] and $GroupOfFour[1], and some excess
//numbers. Function should return array(6,1), If there is not a
//matching case the function should return -1.
//i've realised that the exception cases and the multiple
//grouping does not work.

$GroupOfFour = array (
                     array(3,2,7,6),
                     array(7,6,15,14),
                     array(15,14,11,10),
                     array(1,3,5,7),
                     array(5,7,13,15),
                     array(13,15,9,11),
                     array(0,1,4,5),
                     array(4,5,12,13),
                     array(12,13,8,9),
                     array(0,4,12,8),
                     array(1,5,13,9),
                     array(3,7,15,11),
                     array(2,6,14,10),
                     array(0,1,3,2),
                     array(4,5,7,6),
                     array(12,13,15,14),
                     array(8,9,11,10),
                     array(0,2,8,10),
                     array(0,1,8,9),
                     array(1,3,9,11),
                     array(3,2,11,10),
                     array(0,4,2,6),
                     array(4,12,6,14),
                     array(12,14,8,10)
                     );

function searchFourTerms($leftArray, $GroupOfFour) {
    global $GroupOfFour, $leftArray;
    $len4 = count($leftArray);
    $len4_carry = 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) || ($len4_carry<4))
        return - 1;
    return key($list4);
}

?>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
mozcelikors
  • 2,582
  • 8
  • 43
  • 77

3 Answers3

2

key($list4) will only return the current index of list4. In your case it would return 18.

Try this, it worked for me. Instead of returning key($list4) I returned an array of matching indexes.

function searchFourTerms($la, $gof) {
$i3=0;
if(count($la)<4){
    return -1;
}
$list4 = array();
for($i4 = 0; $i4 < count($gof); $i4++) {
    $intercept4 = array_intersect($gof[$i4], $la);
    $len4 = count($intercept4);
    if(count($intercept4)==4) {
        $list4[$i3] = $i4;
        $i3++;
    }
}
if (empty($list4)){
    return - 1;
}
$list5= array();
$i7=0;
for($i4=0; $i4<count($list4); $i4++){
    $i6=0;
    for($i5=0; $i5<count($list4); $i5++){
        if($i4!=$i5){
            $i6+=count(array_intersect($gof[$i4], $gof[$i5]));
        }
    }
    if($i6<count($gof[$i4])){
        $list5[$i7]=$list4[$i4];
        $i7++;
    }
}
return $list5;
}

if $leftArray = array(0,1,3,2,7,6,8,9), doing a var_dump on the returned array prints the following array, which excludes [13] since it contains all duplicates from [0] and [18]:

array(2) {
[0]=>
int(0)
[1]=>
int(18)
}
Steven Leimberg
  • 785
  • 1
  • 4
  • 18
  • yes, it does seem working. let me ask you a simple modification as well. If a group is unnecessary, it shouldnt give its index. For example, $leftArray = 0,1,3,2,7,6,8,9 There are 3 matching cases. [13],[18], and [0]. But elements in the [13] are already covered in the [18] and [0] groups. So [13] is not necessary. I hope you understand :) – mozcelikors Oct 22 '12 at 18:17
  • It works. But I need a simple modification. If elements of a group is already in another groups, that group is unnecessary. That group should be eliminated. I need that behaviour on the code. – mozcelikors Oct 22 '12 at 18:29
  • FYI, updated the function to exclude unnecessary groups. This should answer your question. – Steven Leimberg Oct 22 '12 at 19:32
0

This is all you need to return more than 1 keys

var_dump(searchFourTerms($leftArray, $GroupOfFour));

Output (All Groups use array_slice to pick any 2)

array
  0 => int 18
  1 => int 1
  2 => int 14
  3 => int 6
  4 => int 20

Your modified Function

function searchFourTerms($leftArray, $GroupOfFour) {
    $len4 = count($leftArray);
    $len4_carry = 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) || ($len4_carry < 4))
        return - 1;
    return array_keys($list4);
}
Baba
  • 94,024
  • 28
  • 166
  • 217
0

I have renamed the input variables and reduced the volume of data in the MCVE.

You only need to perform iterated array_intersect() calls. If the output array is not declared, return -1.

Code: (Demo)

$haystack = [7, 6, 14, 15, 8, 0, 1, 4, 5, 9];
$needles = [
    [3, 2, 7, 6],
    [7, 6, 15, 14],
    [15,14,11,10],
    [1,3,5,7],
    [5,7,13,15],
    [13,15,9,11],
    [0,1,4,5],
    [4,5,12,13],
];

function getIndexOfMatchingArrays(array $needles, array $haystack) {
    if (!$haystack) {
        return -1;
    }
    foreach ($needles as $index => $needle) {
        if ($needle === array_intersect($needle, $haystack)) {
            $fullMatch[] = $index;
        }
    }
    return $fullMatch ?? -1;
}

var_export(
    getIndexOfMatchingArrays($needles, $haystack)
);

Output:

array (
  0 => 1,
  1 => 6,
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136