Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php对搜索结果进行排序_Php_Mysql_Sorting_Search Engine - Fatal编程技术网

使用php对搜索结果进行排序

使用php对搜索结果进行排序,php,mysql,sorting,search-engine,Php,Mysql,Sorting,Search Engine,使用mysql的搜索结果,如何使用php对结果进行排序,以首先显示最接近的匹配项 i、 一个用户搜索关键字,我的数据库中有50个包含不同表和字段关键字的结果。他们都很般配。我想先显示具有精确匹配关键字的结果,然后显示具有其他匹配关键字的结果: 1 = "keyword in this result" 2 = "keyword in 10th result" 3 = "keywords in 5th result" 4 = "has keyword in 3rd result" 5 = "this

使用mysql的搜索结果,如何使用php对结果进行排序,以首先显示最接近的匹配项

i、 一个用户搜索关键字,我的数据库中有50个包含不同表和字段关键字的结果。他们都很般配。我想先显示具有精确匹配关键字的结果,然后显示具有其他匹配关键字的结果:

1 = "keyword in this result"
2 = "keyword in 10th result"
3 = "keywords in 5th result"
4 = "has keyword in 3rd result"
5 = "this has the keyword in the 8th result"
6 = "this has mykeyword in the 40th result"

一旦将记录集恢复到PHP,就可以使用usort轻松地对数组进行排序:


这与字符串中的关键字位置相匹配,因此更相关的字符串将显示得更高。对这一思想的一些递归和修改可以很容易地实现到您的应用程序中。

另一种方法可能是Levenshtein距离,因此您可以按升序距离进行排序。但不确定这是否与问题完全相符……Levenshtein用于拼写错误。以字符串Cars为例-levenshtein会根据与单词的“距离”找到它认为你的意思:crash、care、scar等。抱歉-阅读多维数组usort上的文档:-
<?php
$keyword = 'foobar';
function cmp($a, $b)
{
    if (strpos($a, $keyword) == strpos($b, $keyword)) {
        return 0;
    }
    return (strpos($a, $keyword) < strpos($b, $keyword)) ? -1 : 1;
}

$arr = $my_recordset;
usort($arr, "cmp");

foreach ($arr as $key => $value) {
    echo "$key: $value\n";
}
?>