Home > Backend Development > PHP Tutorial > 统计这个文件里面每个数字重复出现的次数,该如何处理

统计这个文件里面每个数字重复出现的次数,该如何处理

WBOY
Release: 2016-06-13 10:07:00
Original
852 people have browsed it

统计这个文件里面每个数字重复出现的次数
1生成一个10000行文本文件,每行一个随机数字
2统计这个文件里面每个数字重复出现的次数

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->//求更有效率的方法<?php //1生成一个10000行文本文件,每行一个随机数字for($i=0;$i<10000;$i++){file_put_contents("1.txt",rand(0,1000)."\n",FILE_APPEND);}//2统计这个文件里面每个数字重复出现的次数$a=fopen("1.txt","r");$list=array();$tj=array();while($b=fgets($a)){    $list[]=$b;    if(in_array($b,$list))    {        $tj[$b]+=1;    }}//$re=array_count_values($list); //不能使用这个函数echo "<pre class="brush:php;toolbar:false">";var_dump($tj);echo "
Copy after login
";


------解决方案--------------------
按你的思路,统计部分,这样更好,

PHP code
while($b=fgets($a)){        if(isset($list[$b]))    {        ++$list[$b];    }    else    {        $list[$b]    = 1;    }}<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template