How to get best match with php?

Hello there! I have a weird question for one of my projects. …

I have multiple lines with random 9 numbers that i want to find best match.
For example:

1 2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10 11
8 9 10 11 12 13 14 15 16

In this case in my opinion best match is 3 4 5 6 7 8 9 10 11

My question is how to get this result with php? (maybe even multiple results)

I do not have any idea where to start and google search did not helped!

Any ideas?

By “best match” do you mean the line that contains the most numbers in common?

@Gandalf From all 3 lines combining i have to make new 9 number line that uses most used numbers from these 3 lines

a litte harsh, but as a start:

<?php

$numbers = [range(1,9), range(3,11), range(8,16)];

foreach($numbers as $x => $xx){
    foreach($numbers as $y => $yy){
        $matches[$x][$y] = count(array_intersect($xx, $yy));
    }
}

print_r($matches);

but i still don’t really understand what “best match” means here. Better you give a full example.

http://php.net/array-count-values

@chorn take a look at this

-----|—BEST MATCH----| <<<
1 2 |3 4 5 6 7 8 9---------|
-----|3 4 5 6 7 8 9 10 11-|
-----| ----------- 8 9 10 11-| 12 13 14 15 16
-----| BEST MATCH -----| <<<

Best match (9 numbers) here is :3 4 5 6 7 8 9 10 11

Thanks but this just gives me count of each number but what i want is to get bets match based on multiple strings (lets say that i want to select that pars that match in most lines)

the best match is what has the highest count (according to post #6).

therefore: http://php.net/sort, http://php.net/array-keys, & http://php.net/array-slice

You might also want to consider using http://php.net/manual/en/function.similar-text.php convert each array to a string, then compare them.

whats with

01 02 03 04 05 06 07
01 02 03          07
01 02       05 06 07

What do you mean?

what would be the “best match” there?

obviously 01 02 03 04 05 06 07 just in these lines there are only 7 numbers

But “most used numbers” is 1, 2, 7

Yeah but i still need to use all 7 numbers

And what if you do not have enough numbers? You said they are random. Maybe you have to define more test cases and make more precise specifications of what you want achieve. What did you learn from the code i posted?

Every time there will be 9 numbers in line so new number made is also with 9 numbers using numbers that have biggest match in all lines (even if one of the numbers have only 1 count)

What’s the problem with my code then? What did you try by yourself in the meantime?

It did not retur this:

Also about what i have tried:

Of course not, that’s not a blackbox and i won’t do that least bit of work for you - but i provided some code to rely on. Again: What did you learn from the code i posted? What are your further steps?