How to get best match with php?


#1

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?


#2

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


#3

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


#4

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.


#5

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


#6

@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


#7

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)


#8

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


#9

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.


#10

whats with

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

#11

What do you mean?


#12

what would be the "best match" there?


#13

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


#14

But "most used numbers" is 1, 2, 7


#15

Yeah but i still need to use all 7 numbers


#16

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?


#17

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)


#18

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


#19

It did not retur this:

Also about what i have tried:


#20

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?