2016-01-18 44 views
0

我遇到了一個問題,需要計算給定數組中出現單詞的次數。計算單詞的出現次數,並且在同一短語中只有1次

我想要的代碼1,增加一個字('instagrammm')的數量,如果這個詞是在相同的短語。

這裏是我的代碼:

$output = array(); 
$buzzes = ['instagramm some text instagramm', 'instagramm some text' , 'instagramm some text' , 'instagramm some text']; 
foreach ($buzzes as $buzz) { 
    $flag = 0; 
    $words = explode(" ",$buzz); 
    foreach ($words as $word) 
    { 
     $word = mb_convert_case($word, MB_CASE_LOWER, "UTF-8"); 
     $word = preg_replace('/^[^A-Za-z0-9\-]|[^A-Za-z0-9\-]$/', '', $word); 

     if(strlen($word) > 2){ 
      if (array_key_exists($word, $output)){ 
        $output[$word]++; 
      }else{ 
       $output[$word] = 1; 
      }  
     } 
    } 
} 

這裏是預期的結果:

Array 
(
    [instagramm] => 4 
    [some] => 4 
    [text] => 4 
) 
+0

什麼是「在同一個詞組」呢?您能否提供不返回任何內容的示例? (我們可以更好地瞭解該程序的預期行爲。) – onebree

+0

另外,爲什麼當'instagrammm'出現5次時,它會返回4次?你只想計算它是否存在(1+次)或不在字符串中? – onebree

回答

0

看來你是假設在你的代碼的另一個條件。 只是用條件修改了你的代碼。

$output = array(); 
     $buzzes = array('instagramm some text instagramm', 'instagramm some text' , 'instagramm some text' , 'instagramm some text'); 
     foreach ($buzzes as $buzz) { 
      $flag = 0; 
      $words = explode(" ",$buzz); 
      $wordArr = array(); 
      foreach ($words as $word) 
      { 
       $word = mb_convert_case($word, MB_CASE_LOWER, "UTF-8"); 
       $word = preg_replace('/^[^A-Za-z0-9\-]|[^A-Za-z0-9\-]$/', '', $word); 
       if(!in_array(strtolower($word),$wordArr)) { 
        if(strlen($word) > 2){ 
         if (array_key_exists($word, $output)){ 
           $output[$word]++; 
         }else{ 
          $output[$word] = 1; 
         }  
        } 
        $wordArr[] = strtolower($word); 
       } 
      } 
     } 
print_r($output); 
+0

THX是s進出口尋找:) – user492642

0

下面的代碼的簡化版本:

$output = array(); 
$buzzes = ['instagramm some text instagramm', 'instagramm some text' , 'instagramm some text' , 'instagramm some text']; 

foreach(explode(' ', implode(' ', $buzzes)) as $word) 
{ 
    $word = strtolower(trim($word)); 

    if(!empty($word)) 
    { 
     if(isset($output[ $word ])) 
     { 
      $output[ $word ]++; 
     } 
     else{ $output[ $word ] = 1; } 
    } 
} 

輸出:

Array 
(
    [instagramm] => 5 
    [some] => 4 
    [text] => 4 
) 
+0

THX對您有所幫助的,但我想在instagramm但NVM ameenulla0007 4次給我的解決辦法 – user492642

1

試試這一個。這將是用更少的比較和分配速度更快:

foreach ($buzzes as $buzz) { 
    $words = array_unique(explode(' ',$buzz)); 
    foreach ($words as $word) { 
     $word = mb_convert_case($word, MB_CASE_LOWER, "UTF-8"); 
     $word = preg_replace('/^[^A-Za-z0-9\-]|[^A-Za-z0-9\-]$/', '', $word); 
     if(strlen($word) > 2){ 
      if (array_key_exists($word, $output)){ 
       $output[$word]++; 
      }else{ 
       $output[$word] = 1; 
      } 
     } 
    } 
} 

看看:

// My version 
Took: 0.178099 
Array 
(
    [instagramm] => 4 
    [some] => 4 
    [text] => 4 
) 

// accepted version 
Took: 25.308847 
Array 
(
    [instagramm] => 4 
    [some] => 4 
    [text] => 4 
) 
相關問題