Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:我想计算文本文件中特定数量单词的出现次数_Php - Fatal编程技术网

PHP:我想计算文本文件中特定数量单词的出现次数

PHP:我想计算文本文件中特定数量单词的出现次数,php,Php,…(等等) 我想要这样: if occurance are 0 times. world occurance are 0 times. if occurance are 0 times. world occurance are 0 times. if occurance are 0 times. world occurance are 0 times. if occurance are 1 times. world occurance are 0 times. if occurance are

…(等等)

我想要这样:

if occurance are 0 times.
world occurance are 0 times.
if occurance are 0 times.
world occurance are 0 times.
if occurance are 0 times.
world occurance are 0 times.
if occurance are 1 times.
world occurance are 0 times.
if occurance are 0 times.
world occurance are 0 times.
if occurance are 0 times.
world occurance are 0 times.
if occurance are 0 times.
world occurance are 0 times.
if occurance are 0 times.
world occurance are 0 times.
if occurance are 0 times.
world occurance are 0 times.
if occurance are 0 times.
world occurance are 0 times.
if occurance are 0 times.
world occurance are 0 times.
if occurance are 0 times.
world occurance are 0 times.
if occurance are 0 times.
world occurance are 0 times.

如果您将整个单词列表放入一个数组中,您可以使用

if occurance are 47 times.
world occurance are 7 times.
有关更多信息,请参阅(或在一行中阅读)。然后,
$counts
将类似于

$counts = array_count_values($count_array)
然后,您可以循环查看该列表,检查该单词是否在您的单词列表中,并像

Array
(
    [if] => 47
    [world] => 7
    [otherword] => 17
)
试试这个:

$count_array = array("if", "world"); //words to search for and count

$word_counts = array();
//initialize the array like [word] => 0
foreach($count_array as $w) {
    $word_counts[$w] = 0;
}

$file = fopen('Data.txt', "r");
while(!feof($file))
{
    $line = trim(fgets($file));
    if ($line != "") {
        $OBJ = json_decode($line);
        $words = str_word_count($OBJ->user->text, 1); //return associative array of words

        foreach($words as $w) { //go through the list of returned words
            if (in_array($w, $count_array)) { //if this word is in our $count_array
                $word_counts[$w]++; //count it!
            }
        }
    }
}

foreach($word_counts as $word => $count) {
    echo '<b>' . $word . ' occurance are ' . $count . " times.</b><br />";
}
结果:

asd lol rotflol if
world rotflol world
bubu hehe gnigni if if
if hehe if world

file\u get\u content()
是一次性读取整个文件的更好选择吗?如果您将整个单词列表放入一个数组,您只需使用
array\u count\u值($count\u array)
(有关更多信息,请参阅)我不明白你为什么在文本文件中使用
json\u decode
:-/你能提供一个
Data.txt
所包含内容的示例吗?没错!谢谢,先生。非常感谢。
foreach($count_array as $word) {
    echo $word.'</b> occurrence is '.intval($counts[$word]).' times.<br>';
}
$count_array = array("if", "world"); //words to search for and count

$word_counts = array();
//initialize the array like [word] => 0
foreach($count_array as $w) {
    $word_counts[$w] = 0;
}

$file = fopen('Data.txt', "r");
while(!feof($file))
{
    $line = trim(fgets($file));
    if ($line != "") {
        $OBJ = json_decode($line);
        $words = str_word_count($OBJ->user->text, 1); //return associative array of words

        foreach($words as $w) { //go through the list of returned words
            if (in_array($w, $count_array)) { //if this word is in our $count_array
                $word_counts[$w]++; //count it!
            }
        }
    }
}

foreach($word_counts as $word => $count) {
    echo '<b>' . $word . ' occurance are ' . $count . " times.</b><br />";
}
<?php

$count_array = ["if" => 0,"world" => 0];

$file = fopen('Data.txt', "r");

while(!feof($file))
{
    $line = trim(fgets($file));

    $words = explode(" ", $line);

    foreach($words as $word) {
        if (array_key_exists($word, $count_array)) {
            $count_array[$word]++;
        }
    }
}

foreach ($count_array as $word => $number) {
    echo $word . " occurred " . $number . " times" . PHP_EOL;
}
asd lol rotflol if
world rotflol world
bubu hehe gnigni if if
if hehe if world
$ php script.php
if occurred 5 times
world occurred 3 times