0

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

例如

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
4

2 回答 2

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);

    }
}
于 2013-01-16T06:22:22.757 回答
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);
于 2013-01-16T06:31:32.020 回答