Heim > Artikel > Backend-Entwicklung > php处理俩个文本的效率问题
<?php/*==> 1.txt <==a:123b:1333c:333==> 2.txt <==a:3333aa:3433c:323dfa==> result.txt <==a:123:3333c:333:323dfa*/$file_1 = "1.txt";$file_2 = "2.txt";$f = fopen("$file_1", 'r') or die("Cann't Open the file.");while (!(feof($f))) { $line = explode(':', trim(fgets($f))); $f2 = fopen("$file_2", 'r') or die("Cann't Open the file."); while (!(feof($f2))) { $line2 = explode(':', trim(fgets($f2))); if ($line[0] == $line2[0]) { $line[] = $line2[1]; $aaaa = implode(":",$line); $output_file = fopen("./result.txt", 'a'); echo "$aaaa\n"; fwrite($output_file, "$aaaa\n"); fclose($output_file); } }}?>
$t = file('data/1.txt', FILE_IGNORE_NEW_LINES);foreach($t as $v) { list($k, $v) = explode(':', $v); $a[$k][] = $v;}$t = file('data/2.txt', FILE_IGNORE_NEW_LINES);foreach($t as $v) { list($k, $v) = explode(':', $v); $b[$k][] = $v;}foreach($a as $k=>$v) { if(isset($b[$k])) { file_put_contents('data/result.txt', join(':', array_merge(array($k), $v, $b[$k])). PHP_EOL, FILE_APPEND); }}
$t = file('data/1.txt', FILE_IGNORE_NEW_LINES);foreach($t as $v) { list($k, $v) = explode(':', $v); $a[$k][] = $v;}$t = file('data/2.txt', FILE_IGNORE_NEW_LINES);foreach($t as $v) { list($k, $v) = explode(':', $v); $b[$k][] = $v;}foreach($a as $k=>$v) { if(isset($b[$k])) { file_put_contents('data/result.txt', join(':', array_merge(array($k), $v, $b[$k])). PHP_EOL, FILE_APPEND); }}
会有这种事?文件并不大嘛。
可能是其他的原因
你把这两个文件放到云盘上
直接设置 memory_limit = 1024M,速度快多了,非常感谢@xuzuning。