开发者

Array of most occurring word in a file separated by newlines.

How would I get an array of the top occurring words out of a file separated by newlines (\n)?

Example file:

person
person
dog
cat
person
lemon
orange
person
cat
dog
dog

The words in the file are in no particular order.

How woul开发者_如何学编程d I make it behave like the following?

echo $top[0]; //output: person

echo $top[1]; //output: dog

etc...

Thanks in advance!


$lines = file("theFile.txt");
var_dump(array_count_values($lines));

http://php.net/array_count_values

Demo: http://ideone.com/zd82W

To get the first element (word which occurs the most) from the resulting array, you can do this:

$arr = array("person", "person", "cat", "dog", "cat");
$newArr = array_count_values($arr);
echo key($newArr); // "person"

Demo: http://ideone.com/A0WPa


I would probably use something like this :

  • read the file line by line, adding +1 to an array item each time a word is detected, counting for each word how many times it's been seen
  • sorting that array.


Not really tested, but something like this should work, I suppose :
(should work better than array_count_values() if your file is big : no need to load the whole file into memory)

$words = array();

$f = fopen('your_file', 'r');
while ($line = fgets($f)) {
    $word = trim($line);
    if (isset($words[$words])) {
        $words[$words]++;
    }
    else {
        $words[$words] = 1;
    }
}

asort($words);

Now, the first key in the $words array is the most used word -- and the corresponding value is the number of times it's been seen in your file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜