Php 如何从数组中删除重复的单词?

Php 如何从数组中删除重复的单词?,php,arrays,unique,Php,Arrays,Unique,我有一个数据库查询,可以提取文本字符串 $descriptionsQuery = mysql_query("select prob_text from opencall where logdatex between $OneHourAgo and $TimeNow ORDER by callref DESC") or die(mysql_error()); $descriptions = array(); while ($row = mysql_fetch_assoc($description

我有一个数据库查询,可以提取文本字符串

$descriptionsQuery = mysql_query("select prob_text from opencall where logdatex between $OneHourAgo and $TimeNow ORDER by callref DESC") or die(mysql_error());
$descriptions = array();

while ($row = mysql_fetch_assoc($descriptionsQuery)){
$descriptions[] = $row['prob_text'];
}
//put all the strings together with a space between them
$glue = implode (" ",$descriptions);
我需要帮助的是。。。在“descriptions[]”被“粘合”成一个长字符串之前,我希望删除任何重复的单词。一旦它们粘在一起,我就依赖于每个原始描述中都有重复的单词。这有点难以解释,这里有一个例子来说明我的意思。2用户输入一些文本,例如 用户1:
“我与利兹服务器有问题。我在利兹”

用户2:
“利兹的玛格丽特有问题,请打电话给玛格丽特”
。因此,我希望User1在最终的粘合字符串中只有1个“Leeds”,User2只有1个margaret,但是两个用户都提到“Leeds”,所以我希望粘合字符串中有两个,每个用户一个。这可能吗?非常感谢您的帮助。

您可以使用
$newarray=array\u unique($oldarray)
实现这一点

首先分解每一行以获得一个数组。使用
array\u unique()
删除重复项。然后内爆每一行,然后内爆所有行

$descriptionsQuery = mysql_query("select prob_text from opencall where logdatex between $OneHourAgo and $TimeNow ORDER by callref DESC") or die(mysql_error());
$descriptions = array();

while ($row = mysql_fetch_assoc($descriptionsQuery)){
  $tmp = explode(' ', $row['prob_text']);
  $tmp = array_unique($tmp);
  // or case insensitive
  // $tmp = array_intersect_key($array,array_unique(array_map(strtolower,$array)));
  $descriptions[] = implode(' ', $tmp);
}
//put all the strings together with a space between them
$glue = implode (" ",$descriptions);

如果要以不区分大小写的方式删除重复项,则必须在while中更改第二行。我在这里找到了一些提示:

您可以使用
$newarray=array\u unique($oldarray)
实现这一点

首先分解每一行以获得一个数组。使用
array\u unique()
删除重复项。然后内爆每一行,然后内爆所有行

$descriptionsQuery = mysql_query("select prob_text from opencall where logdatex between $OneHourAgo and $TimeNow ORDER by callref DESC") or die(mysql_error());
$descriptions = array();

while ($row = mysql_fetch_assoc($descriptionsQuery)){
  $tmp = explode(' ', $row['prob_text']);
  $tmp = array_unique($tmp);
  // or case insensitive
  // $tmp = array_intersect_key($array,array_unique(array_map(strtolower,$array)));
  $descriptions[] = implode(' ', $tmp);
}
//put all the strings together with a space between them
$glue = implode (" ",$descriptions);

如果要以不区分大小写的方式删除重复项,则必须在while中更改第二行。我在这里找到了一些提示:
最好是在查询中执行

你可以这样做

SELECT DISTINCT prob_text FROM opencall WHERE logdatex BETWEEN $OneHourAgo AND $TimeNow ORDER BY callref DESC
这将只在数据库中选择一次单词,因此您不会选择任何重复的单词


最好是在查询中执行

你可以这样做

SELECT DISTINCT prob_text FROM opencall WHERE logdatex BETWEEN $OneHourAgo AND $TimeNow ORDER BY callref DESC
这将只在数据库中选择一次单词,因此您不会选择任何重复的单词

使用。或者在查询中使用DISTINCT

$descriptionsQuery = mysql_query("select prob_text from opencall where logdatex between $OneHourAgo and $TimeNow ORDER by callref DESC") or die(mysql_error());
$descriptions = array();

while ($row = mysql_fetch_assoc($descriptionsQuery)){
$descriptions[] = $row['prob_text'];
}

//remove duplicates:
$descriptions = array_unique($descriptions);

//put all the strings together with a space between them
$glue = implode (" ",$descriptions);
使用。或者在查询中使用DISTINCT

$descriptionsQuery = mysql_query("select prob_text from opencall where logdatex between $OneHourAgo and $TimeNow ORDER by callref DESC") or die(mysql_error());
$descriptions = array();

while ($row = mysql_fetch_assoc($descriptionsQuery)){
$descriptions[] = $row['prob_text'];
}

//remove duplicates:
$descriptions = array_unique($descriptions);

//put all the strings together with a space between them
$glue = implode (" ",$descriptions);

似乎是一个很好的时间来使用和。这将过滤掉单个消息中的所有重复单词,忽略大小写:

// $chat is the db result array
foreach($chat as &$msg) {
    $final = [];
    array_walk(str_word_count($msg, 1), function($word) use (&$final) {
        if (!in_array(strtolower($word), array_map('strtolower', $final))) {
            $final[] = $word;
        }
    });
    $msg = implode(' ', $final);
});        
$filtered = implode(' ', $chat);

注意使用
str\u word\u count()
而不是
explode()
。我还没有在生产环境中测试过它,但是它会去掉基本的标点符号(除了
'
-
);当您尝试创建标记云时,可能会很有用。

似乎是使用和删除标记云的好时机。这将过滤掉单个消息中的所有重复单词,忽略大小写:

// $chat is the db result array
foreach($chat as &$msg) {
    $final = [];
    array_walk(str_word_count($msg, 1), function($word) use (&$final) {
        if (!in_array(strtolower($word), array_map('strtolower', $final))) {
            $final[] = $word;
        }
    });
    $msg = implode(' ', $final);
});        
$filtered = implode(' ', $chat);

注意使用
str\u word\u count()
而不是
explode()
。我还没有在生产环境中测试过它,但是它会去掉基本的标点符号(除了
'
-
);当您尝试创建标记云时可能会很有用。

为什么不简单地将SQL查询更改为
从opencall中选择DISTICT prob_text
?因此您希望最后一个字符串为:“我与Leeds服务器有问题。我在Margaret有问题,请致电”?@guillaume royer是您需要的答案…@MatW,我希望是这样的“我对利兹服务器有问题。我在利兹的玛格丽特有问题,请致电“-如果两个用户说同一个词,那么它会出现两次(3个用户,3次等)但是,如果一个用户说了两次,另一个用户说了一次,我只想在那里说两次,每个用户说一次,为什么不简单地将SQL查询更改为
从opencall选择DISTICT prob_text
?因此,您希望最后的字符串为:“我与Leeds服务器有问题。我在Margaret有问题,请致电“?@guillaume royer是您需要的答案…@MatW,我希望是“我与利兹服务器有问题。”。我在利兹的玛格丽特有问题,请致电“-如果两个用户说同一个词,那么它会出现两次(3个用户,3次等),但是,如果一个用户说了两次,另一个说了一次,我只想在那里说两次,每个用户说一次,这不是问的问题:/这不是问的问题:/这是区分大小写的,所以Margaret和Margaret将在最后一个字符串中这是区分大小写的,所以Margaret和Margaret将在最后一个字符串中