首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >php删除数组中的重复字词

php删除数组中的重复字词
EN

Stack Overflow用户
提问于 2013-04-26 02:04:45
回答 5查看 990关注 0票数 2

对不起,英语不是我的母语,可能题目不太好。我想做这样的事情。

代码语言:javascript
复制
$str = array("Lincoln Crown","Crown Court","go holiday","house fire","John Hinton","Hinton Jailed");

这里是一个数组,“林肯皇冠”包含“林肯”和“皇冠”,所以删除下一个单词,其中包含这两个单词,并且"Crown Court(包含皇冠)“已被删除。

在另一种情况下。"John Hinton“包含"John”和"Hinton",因此已删除"Hinton被监禁(包含Hinton)“。最终输出应如下所示:

代码语言:javascript
复制
$output = array("Lincoln Crown","go holiday","house fire","John Hinton");

因为我的php技术不好,它不是简单的使用array_unique() array_diff(),所以打开一个问题寻求帮助,谢谢。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-04-26 02:25:04

我想这可能行得通:

代码语言:javascript
复制
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));

转储:

代码语言:javascript
复制
Array
(
    [0] => Lincoln Crown
    [2] => go holiday
    [3] => house fire
    [4] => John Hinton
)
票数 2
EN

Stack Overflow用户

发布于 2013-04-26 02:13:48

看起来您需要一个循环,然后在数组中构建一个单词列表。

像这样:

代码语言:javascript
复制
<?
// 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);
    }
}
?>
票数 2
EN

Stack Overflow用户

发布于 2013-04-26 02:21:38

就像我在你的案例中所做的:

代码语言:javascript
复制
$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);

输出:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16221657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档