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);
}
?>
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)
Output: