I create a tag based search and want to sort the search result according to the relevance. The best fit and the number of tags are relevant. Imagine this is the search query:
$search = ['A', 'B'];
and this the result:
$result = [
[
'id' => 3011,
'tags' => ['A', 'B', 'C']
],
[
'id' => 10798,
'tags' => ['A','C','D','E']
],
[
'id' => 92,
'tags' => ['A']
],
[
'id' => 4237,
'tags' => ['A', 'B']
]
];
I want to reorganize the whole thing as follows:
$sortResult = [
[
'id' => 4237,
'tags' => ['A', 'B'] // At first place because has 2 values and the search 2, difference 0 and matched good to search
],
[
'id' => 3011,
'tags' => ['A', 'B', 'C'] // At second place because has 3 values and the search 2, difference 1 and matched good to search
],
[
'id' => 92,
'tags' => ['A'] // At third place because has 1 value and the search 2, difference 1
],
[
'id' => 10798,
'tags' => ['A','C','D','E'] // Down here because has 4 values and the search 2, difference 2
]
];
Not sure if this works in all cases but it gives the desired result for your current data:
http://phpio.net/s/7yhb