我希望编写一个PHP函数,该函数接受一个字符串作为输入,将其拆分为单词,并返回一个按照每个单词出现频率排序的单词数组。最有效的算法是什么?这里有一个类似的问题:php: sort and count instances of words in a given string。
以下是您最好的选择:
示例
$words = 'A string with certain words occuring more often than other words.';
print_r( array_count_values(str_word_count($words, 1)) );
输出
Array
(
[A] => 1
[string] => 1
[with] => 1
[certain] => 1
[words] => 2
[occuring] => 1
[more] => 1
[often] => 1
[than] => 1
[other] => 1
)
因为该问题是至少两个包含相同答案的其他问题的重复,所以标记为CW