时间:2021-07-01 10:21:17 帮助过:21人阅读
/*
==> 1.txt <==
a:123
b:1333
c:333
==> 2.txt <==
a:3333
aa:3433
c:323dfa
==> result.txt <==
a:123:3333
c: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);
}
}