下面的代码基本上将计算一个单词在数组中出现的次数。
不过,我现在想要做的是获取$word
,将其分配为数组的键,然后分配$WordCount[$word]
为其值。例如,如果我得到一个单词“jump”,它将自动分配为数组的键,并将单词“jump”( $WordCount[$word]
) 的出现次数分配为其值。请问有什么建议吗?
function Count($text)
{
$text = strtoupper($text);
$WordCount = str_word_count($text, 2);
foreach($WordCount as $word)
{
$WordCount[$word] = isset($WordCount[$word]) ? $WordCount[$word] + 1 : 1;
echo "{$word} has occured {$WordCount[$word]} time(s) in the text <br/>";
}
}