如何删除字符串中的重复单词,这个字符串是数组的值?

时间:2013-01-16 06:12:50

标签: php

如何删除字符串中的重复单词,其中此字符串是数组的值?

e.g

foreach($results as $result)
{
   foreach($result as $words)
    {
        echo $words = str_word_count($words,0)."\n";
    }
}

结果是例如

test  = 1
test activity = 2
test CI to CGI = 4
Test car Pool = 3

我想要发生的是,例如删除单词“test”的其他重复项并列出所有unqiue单词,以便结果只有

test = 1
activity = 1
CI = 1
to = 1
CGI = 1
car = 1
pool = 1

2 个答案:

答案 0 :(得分:0)

$final=array();

foreach($results as $result)
{
   foreach($result as $key=> $words)
    {
        echo $words = str_word_count($words,0)."\n";

       if(!in_array($words,array_keys($final))
           $final[$words]=str_word_count($words,0);

    }
}

答案 1 :(得分:0)

$results=array(
               array(
               "test",
               "test activity",
               "test CI to CGI",
               "Test car Pool"
               )
              );
$ws=array();
foreach($results as $result)
{
   foreach($result as $words)
    {
        $arr=explode(' ',$words);
        foreach($arr as $word)
        {
           if($word!='')
           {
              if(!isset($ws[$word])) $ws[$word]=1;
              else $ws[$word]++;
           }
        }
    }
}

print_r($ws);