- 论坛徽章:
- 1
|
本帖最后由 这个冬天不冷 于 2014-12-02 15:28 编辑
- <?php
- error_reporting(0);
- ini_set('memory_limit','2048M'); //修改php支持的最大内存
- $fp1 = fopen("file1.txt", "r") or die('no file1');
- $fp2 = fopen("file2.txt", "r") or die('no file2');
- $contents1 = fread($fp1, filesize('file1.txt'));
- $contents2 = fread($fp2, filesize('file2.txt'));
- $arr1 = explode("\n", $contents1);
- $arr2 = explode("\n", $contents2);
- $t1 = array();
- $t2 = array();
- foreach ($arr1 as $key => $value) {
- if(!empty($value))
- $a_tmp = explode(" ", $value);
- $t1[$a_tmp[0]] = end($a_tmp);
- }
- foreach ($arr2 as $key => $value) {
- if(!empty($value))
- $a_tmp = explode(" ", $value);
- $t2[$a_tmp[0]] = end($a_tmp);
- }
- //统计结果
- $xiangdeng = array();
- $cha1 = array();
- $cha2 = array();
- $no1 = array();
- $no2 = array();
- foreach ($t1 as $key => $value) {
- if($t1[$key] > $t2[$key] && isset($t2[$key])) $cha1[count($cha1)] = "$key file1biggerfile2 $t1[$key]";
- if($t1[$key] < $t2[$key] && isset($t1[$key])) $cha2[count($cha2)] = "$key file2biggerfile1 $t2[$key]";
- if($t1[$key] == $t2[$key]) $xiangdeng[count($cha2)] = "$key equal $t2[$key]";
- }
- //求只在文件2 中出现的
- foreach ($t2 as $key => $value) {
- if(!isset($t1[$key])) $no2[count($no2)] = "$key onlyinfile2 $t2[$key]";
- }
- foreach ($t1 as $key => $value) {
- if(!isset($t2[$key])) $no1[count($no2)] = "$key onlyinfile1 $t1[$key]";
- }
- $str = implode("\r\n", $xiangdeng);
- file_put_contents("result1", $str);
- $str = implode("\r\n", $cha1);
- file_put_contents("result2", $str);
- $str = implode("\r\n", $cha2);
- file_put_contents("result3", $str);
- $str = implode("\r\n", $no1);
- file_put_contents("result4", $str);
- $str = implode("\r\n", $no2);
- file_put_contents("result5", $str);
- echo '<pre>';
- print_r($xiangdeng);
- print_r($cha1);
- print_r($cha2);
- print_r($no1);
- print_r($no2);
- fclose($fp1);
- fclose($fp2);
复制代码- Array
- (
- [0] => 1221 equal 2453
- )
- Array
- (
- [0] => 1223 file1biggerfile2 5687
- )
- Array
- (
- [0] => 1245 file2biggerfile1 5621
- )
- Array
- (
- [2] => 1243 onlyinfile1 7683
- )
- Array
- (
- [0] => 1265 onlyinfile2 8000
- [1] => 1287 onlyinfile2 4321
- )
复制代码 文件都不贴了,文件就是根据上面的数组运行结果 ,写入的 |
|