2013-04-25 53 views
2

對不起,英語不是我的母語,也許問題標題不太好。我想要做這樣的事情。php刪除數組中的重複單詞

$str = array("Lincoln Crown","Crown Court","go holiday","house fire","John Hinton","Hinton Jailed"); 

這裏是一個數組,「林肯冠」含有「林肯」和「冠」,所以刪除接下來的話,巫包含這些2個字,和「官方法院(包含皇冠)」已被刪除。

另一種情況。 「約翰Hinton」包含「約翰」和「Hinton」,因此「Hinton監獄(包含Hinton)」被去除了。最終的輸出應該是這樣的:

$output = array("Lincoln Crown","go holiday","house fire","John Hinton"); 

爲我的PHP技能並不好,它不是簡單地使用array_unique()和array_diff(),所以打開一個問題尋求幫助,謝謝。

+1

除了你的問題,什麼是 「考特尼·考克斯」 和「考克斯電纜「 - 完全不同的應用考克斯這個詞。如果這適用於這種情況適用的情況,則可能不希望僅基於一個詞的存在而排除。 – 2013-04-25 18:09:53

回答

2

我想這可能工作:P

function cool_function($strs){ 
    // Black list 
    $toExclude = array(); 

    foreach($strs as $s){ 
     // If it's not on blacklist, then search for it 
     if(!in_array($s, $toExclude)){ 
      // Explode into blocks 
      foreach(explode(" ",$s) as $block){ 
       // Search the block on array 
       $found = preg_grep("/" . preg_quote($block) . "/", $strs); 
       foreach($found as $k => $f){ 
        if($f != $s){ 
         // Place each found item that's different from current item into blacklist 
         $toExclude[$k] = $f; 
        } 
       } 
      } 
     } 
    } 

    // Unset all keys that was found 
    foreach($toExclude as $k => $v){ 
     unset($strs[$k]); 
    } 

    // Return the result 
    return $strs; 
} 

$strs = array("Lincoln Crown","Crown Court","go holiday","house fire","John Hinton","Hinton Jailed"); 
print_r(cool_function($strs)); 

轉儲:

Array 
(
    [0] => Lincoln Crown 
    [2] => go holiday 
    [3] => house fire 
    [4] => John Hinton 
) 
+0

+1我仍然試圖使我自己的版本,並失敗:) – samayo 2013-04-25 18:33:25

+0

我認爲這是一個瑣事,而不是一個可用的代碼。 – 2013-04-25 18:37:02

0

您可以在原始數組中使用explode每個字符串,然後使用循環比較每個單詞(將一個數組中的每個單詞與另一個數組中的每個單詞進行比較,如果匹配,則刪除整個數組)。

2

好像你需要一個循環,然後在數組中建立一個單詞列表。

像:

<? 
// Store existing array's words; elements will compare their words to this array 
// if an element's words are already in this array, the element is deleted 
// else the element has its words added to this array 
$arrayWords = array(); 

// Loop through your existing array of elements 
foreach ($existingArray as $key => $phrase) { 
    // Get element's individual words 
    $words = explode(" ", $phrase); 

    // Assume the element will not be deleted 
    $keepWords = true; 

    // Loop through the element's words 
    foreach ($words as $word) { 
     // If one of the words is already in arrayWords (another element uses the word) 
     if (in_array($word, $arrayWords)) { 
      // Delete the element 
      unset($existingArray[$key]); 

      // Indicate we are not keeping any of the element's words 
      $keepWords = false; 

      // Stop the foreach loop 
      break; 
     } 
    } 

    // Only add the element's words to arrayWords if the entire element stays 
    if ($keepWords) { 
     $arrayWords = array_merge($arrayWords, $words); 
    } 
} 
?> 
0

正如我會做你的情況:

$words = array(); 

foreach($str as $key =>$entry) 
{ 
    $entryWords = explode(' ', $entry); 
    $isDuplicated = false; 
    foreach($entryWords as $word) 
     if(in_array($word, $words)) 
      $isDuplicated = true; 
    if(!$isDuplicated) 
     $words = array_merge($words, $entryWords); 
    else 
     unset($str[$key]); 
} 

var_dump($str); 

輸出:

array (size=4) 
    0 => string 'Lincoln Crown' (length=13) 
    2 => string 'go holiday' (length=10) 
    3 => string 'house fire' (length=10) 
    4 => string 'John Hinton' (length=11) 
0

array_unique()例如

<?php 
$input = array("a" => "green", "red", "b" => "green", "blue", "red"); 
$result = array_unique($input); 
print_r($result); 
?> 

輸出:

Array 
(
    [a] => green 
    [0] => red 
    [1] => blue 
) 

Source