dotty
dotty

Reputation: 41473

see if 3 or more numbers match an array

Hay i have an array consisting for 6 random numbers, here's an example

[4,8,12,22,23,43]

I also have 100 arrays containing 6 numbers, these are all random a few examples could be

[5,8,15,47,32,48]
[3,4,8,12,33,42]
[8,12,26,55,43,33]
[4,63,45,23,45,55] ...

I want to see how many times (out of the array of 100) these numbers match at least 3 from the top array. As you can guess this is a lottery experiment.

As you can see array number 3 matches 3 numbers from the top array.

Any ideas how do do this? With perhaps an option to see if 4 numbers matched.

Upvotes: 1

Views: 189

Answers (2)

k102
k102

Reputation: 8079

maybe smth like this:

$winner = [4,8,12,22,23,43];

$arrays = //all your 100 arrays

$i = 0; // number of matches

foreach ($arrays as $array)
{
    $result = array_intersect($array, $winner);
    if (count($result) >= 3) $i++;
}

Upvotes: 2

user680786
user680786

Reputation:

    $master_array = array(4, 8, 12, 22, 23, 43);

    $arrays = array(array(5, 8, 15, 47, 32, 48),
                array(3, 4, 8, 12, 33, 42),
            array(8, 12, 26, 55, 43, 33),
            array(4, 63, 45, 23, 45, 55));

    foreach ($arrays as $arr)
    {
        $intersect = array_intersect($master_array, $arr);
        if (count($intersect)==3) print 'Match: '.print_r($arr, true).PHP_EOL;
    }

Upvotes: 5

Related Questions