0

I am building a game to check lottery tickets so I am trying to build a loop that will loop a 6 lottery numbers through a list of 50 lottery lines.

I have an array with 6 non duplicated numbers. I want to loop this array through 50 arrays each with 6 numbers but in each array no number can be duplicated.

I would like to return how many times the numbers in array match any of the numbers in any of the other 50 arrays.

1 number = 20 matches
2 numbers = 10 matches
3 numbers = 1 match.

I am new enough to PHP and trying to find the easiest way to do this.

I am using this game to improve my knowledge of PHP, any help would be appreciated.

user1844933
  • 3,296
  • 2
  • 25
  • 42

1 Answers1

0

Try something like this:

<?php
//array to store the number of matches
$matchesArr = array(0,0,0,0,0,0,0);

//your lottery numbers
$myNumbers = array(1,2,3,4,5,6);

//the past lottery results
$pastResults = array(
    array(10,12,1,2,34,11),
    array(10,12,1,2,34,11),
    array(10,12,1,2,34,11)
);

//loop through each past lottery result
foreach($pastResult as $pastResult){
    $matches = 0;

    //do any of your numbers appear in this result?
    foreach($myNumbers as $myNumber){
        if(in_array($myNumber, $pastResult)){
            $matches++;
        }
    }

    //add the number of matches to the array
    $matchesArr[$matches]++;
}

//print the number of matches
foreach($matchesArr as $index=>$matches){
    echo $index." number = ".$matches."\n";
}

?>
user1578653
  • 4,888
  • 16
  • 46
  • 74