2014-01-08 61 views
0

我有翻出文本字符串數據庫查詢如何從數組中刪除重複的單詞?

$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); 

什麼,我想幫助的是......之前「的說明。[]」被「粘」是一個很長的字符串,我我想要刪除任何重複的單詞。一旦他們粘在一起,我就依靠每個原始描述中有重複的單詞。這很難解釋,這裏是我的意思的一個例子。 2個用戶輸入一些文本,例如 用戶1:"I have an issue with Leeds server. I am in Leeds" 用戶2:"Margaret in Leeds has a problem, please call margaret"。因此,我希望User1在最終粘貼的字符串中只有1個「Leeds」,User2只有1個margaret,但是兩個用戶都提到了「Leeds」,所以我希望那裏有兩次膠粘字符串,每個用戶一次。這可能嗎?任何幫助讚賞。

+0

http://uk3.php.net/function.array-unique – putvande

+0

爲什麼不簡單地將您的SQL查詢更改爲'SELECT DISTICT prob_text FROM opencall'? –

+0

請刪除mysql_並移至mysqli_。 mysql_將被PHP刪除是時間問題。 – Mave

回答

5

你可以用$newarray = array_unique($oldarray)來做到這一點。

首先爆炸你的每一行以得到一個數組。使用array_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); 

http://de3.php.net/function.array-unique

如果你想刪除一個不區分大小寫的方式重複,你必須改變的,而第二行。我在這裏找到提示: Best solution to remove duplicate values from case-insensitive array

+0

這是大小寫敏感的,所以瑪格麗特和瑪格麗特將在最後的字符串 – Mathew

+0

完美,謝謝! – Maff

+0

我添加了對大小寫不敏感的編輯。 – Guilro

0

使用array_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)){ 
$descriptions[] = $row['prob_text']; 
} 

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

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

好像使用array_walkanonymous functions的好時機使用DISTINCT。這將過濾掉所有的重複單詞,在一個單一的消息,忽略大小寫:

// $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_word_count()而非explode()。我沒有在生產環境中測試這個,但它會去掉基本的標點符號('-除外)。當您嘗試創建標籤雲時可能會很有用。