将在文本文件中找到的所有值相加并使用 Array_count_values in php 存储它们

Sum all values found in a text file and store them using Array_count_values in php

好吧,我是 php 的新手,所以我正在制作一个程序来计算特定文本文件中的单词。 这是我的文本文件:

Hello Hello Hello Hello
Hello Word array sum
Hello Find

这是我的代码 (php:

/*Open file*/
$handle = fopen($_FILES['file']['tmp_name'], 'r');

/*read all lines*/
while (! feof($handle)) {
$line = fgets($handle);

/*using array_count_values with str_word_count to count words*/
$result=       (array_count_values(str_word_count(strip_tags(strtoupper($line)), 1)));

/*sort array*/
arsort($result);

/*show the first ten positions and print array*/
$top10words2 = array_slice($result, 0, 10);
print "<pre>";
print_r ($top10words2);
print "</pre>";
}
fclose($handle);

但我的输出是这样的:

Array{
[Hello] => 4
}
Array{
[Hello] => 1
[Word] => 1
[array] => 1
[sum] => 1
}
Array{
[Hello] => 1
[Find] => 1
}

我需要这样的输出:

Array{
[Hello] => 6
[Word] => 1
[array] => 1
[sum] => 1
[find] => 1
}

有什么建议吗?

改用file_get_contents

$fileContent = file_get_contents($_FILES['file']['tmp_name']);
/* using array_count_values with str_word_count to count words */
$result = (array_count_values(str_word_count(strip_tags(strtoupper($fileContent)), 1)));
/* sort array */
arsort($result);
/* show the first ten positions and print array */
$top10words2 = array_slice($result, 0, 10);
print "<pre>";
print_r($top10words2);
print "</pre>";

这是输出:

Array
(
    [HELLO] => 6
    [FIND] => 1
    [SUM] => 1
    [ARRAY] => 1
    [WORD] => 1
)

您没有做任何事情来合并您在每一行上计算的字数。通过设置 $result = array_count_values(...),您将取消前一个循环的结果。此外,因为您是在循环内执行拼接和转储,所以您永远不会对完整的结果集采取行动,因此永远不会真正了解前 10 个最常用的词是什么。

您的代码需要进行两处更改:

  1. 将每行的计数合并到一个数组中。
  2. 等到您完成文件处理后再查看 结果。

使用 file_get_contents() 会起作用,但根据您正在处理的文件的大小,这可能会导致内存限制错误。使用您的初始方法的解决方案如下所示:

$results = [];
while (!feof($handle)) {
  $line = fgets($handle);
  $line_results = array_count_values(str_word_count(strip_tags(strtoupper($line)), 1));
  foreach ($line_results as $word => $count) {
    if (isset($results[$word])) {
      $results[$word] += $count;
    }
    else {
      $results[$word] = $count;
    }
  }
}

arsort($results);
// etc...

我同意 Ayaoufile_get_contents() 回答,但是对于非常大的文件,您可能需要在开始时就这样做。您想在循环中构建单词数组,然后 countsortslice

$result = array();
while(!feof($handle)) {
    $line = fgets($handle);
    $result = array_merge($result, str_word_count(strip_tags(strtoupper($line)), 1));
}
$result = array_count_values($result);
arsort($result);
$top10words2 = array_slice($result, 0, 10);