统计这个文件里面每个数字重复出现的次数
1生成一个10000行文本文件,每行一个随机数字
2统计这个文件里面每个数字重复出现的次数
- PHP code
<!---ecms Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->//<strong style="color:transparent">¥本文来源gaodai#ma#com搞@@代~&码网^</strong><small>搞gaodaima代码</small>求更有效率的方法<?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="prettyprint linenums">";var_dump($tj);echo "
“;
——解决方案——————–
按你的思路,统计部分,这样更好,
- PHP code
while($b=fgets($a)){ if(isset($list[$b])) { ++$list[$b]; } else { $list[$b] = 1; }}