2014-01-08 3 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 명의 사용자가 텍스트를 입력합니다 (예 : User1 : "I have an issue with Leeds server. I am in Leeds" User2 : "Margaret in Leeds has a problem, please call margaret"). 이걸로 User1은 마침내 접착제로 묶인 문자열에 1 개만 "Leeds"를 갖게하고 User2는 1 마가렛 만 가질 수 있습니다.하지만 두 사용자 모두 "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()을 사용하십시오. 그런 다음 각 행을 함몰시킨 다음 모두 내 보냅니다.

http://de3.php.net/function.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); 

, 당신은 동안의 두 번째 라인을 변경해야합니다. 여기에서 도움말을 찾았습니다. Best solution to remove duplicate values from case-insensitive array

+0

에서 한 번, 두 번이 원하는라고하지만 경우 (3 사용자, 3 번 등) 이것은 대소 문자를 구별하므로 마가렛과 마가렛은 최종 문자열 – Mathew

+0

에 완벽하게 될 것입니다. 감사합니다! – Maff

+0

대소 문자를 구분하지 않는 편집을 추가했습니다. – Guilro

1

더 나은 것은 쿼리에서하는 것입니다.

당신은 당신이 어떤 중복 선택하지 않도록이 내용은, 데이터베이스에 한 번 단어를 선택합니다

SELECT DISTINCT prob_text FROM opencall WHERE logdatex BETWEEN $OneHourAgo AND $TimeNow ORDER BY callref DESC 

뭔가를 할 수 있습니다.

http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html

+1

질문이 아닙니다. / – 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()의 사용 : 이것은 하나의 메시지에 무시하는 경우 모든 중복 된 단어를 필터링합니다. 나는 이것을 프로덕션 환경에서 테스트하지는 않았지만, 기본적인 구두점을 제거 할 것이다 ('- 제외); 태그 클라우드를 만들려고 할 때 유용 할 수 있습니다.

관련 문제