php在sql数据库中查找最匹配的句子
我正在制作一个问答应用程序,作为其中的一部分,它需要查看存储在SQL数据库中的以前的问题,并找到以前的问题中匹配词最多,然后它将从数据库中获取该行的属性 我已经能够让它为数据库中的每一行生成一个匹配单词的数组。但是我需要一种组织这些数组的方法来选择匹配最多的数组。这是我到目前为止使用过的SQL和PHPphp在sql数据库中查找最匹配的句子,php,mysql,sql,Php,Mysql,Sql,我正在制作一个问答应用程序,作为其中的一部分,它需要查看存储在SQL数据库中的以前的问题,并找到以前的问题中匹配词最多,然后它将从数据库中获取该行的属性 我已经能够让它为数据库中的每一行生成一个匹配单词的数组。但是我需要一种组织这些数组的方法来选择匹配最多的数组。这是我到目前为止使用过的SQL和PHP $questions1 = $_GET['question']; $questionsarray = explode(" ",$questions1); 新的问题变成了一个数组,下面将与ma
$questions1 = $_GET['question'];
$questionsarray = explode(" ",$questions1);
新的问题变成了一个数组,下面将与match的所有其他问题进行比较
$sql = "SELECT * FROM records WHERE userid= '24.9.71.79'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$questionsasked = $row['old_questions'];
// turns old question into an array
$last_q_array = explode(" ",$questionsasked);
//finds matches between the old and new question
$intersectarray = array_intersect($last_q_array,$questionsarray);
然后,它使用array_diff清除常用词,帮助它专注于找到真正的主题
$cleanedarray = array_diff($intersectarray ,$commonwords);
//prints the array if it find matches and sets the var
if(count($cleanedarray)>0) {
print_r($cleanedarray);
$desiredattri = $row[last_answer_type];
echo "<br />----------------<br />";
}
}
}
所以现在我需要找到一种方法来解析这些数组,并找到匹配最多的数组。我可以使用count对每个数组中的匹配项进行计数,但仍然需要将该数字与其余数组计数进行比较,然后将数组的属性用于大多数匹配项 你可以试试这样的东西。它将使用单词本身作为数组键,数组值是计数
$result = array();
foreach ($cleanedarray as $word) {
if (!array_key_exists($word, $result)) {
$result[$word] = 0;
}
$result[$word]++; // keep count using the word as key
}
我相信可能还有其他内置PHP函数可以为您实现这一点,但这是一种快速而肮脏的方式,我立刻想到了这一点。请适当标记您的问题。您使用的是SQL Server还是MySQL?@GordonLinoff很抱歉,我在SQL方面没有太多的专业知识,但我很确定这是MySQL。这看起来像是一个可疑的家庭作业……谢谢你,这非常接近我需要的,我想我需要将它作为一个起点,因为我仍然需要从找到最多匹配项的sql行中提取值。但我想你至少让我走上了正轨。
$result = array();
foreach ($cleanedarray as $word) {
if (!array_key_exists($word, $result)) {
$result[$word] = 0;
}
$result[$word]++; // keep count using the word as key
}