0

提前感谢您在这个问题上帮助我,

我有两个文件

file1.txt其中包含:

adam
william
Joseph
Hind 
Raya 

file2.txt其中包含:

Student
Teacher

我想要的是以这种方式将两个文件合并到一个文件中,这样当eof到达file2.txt时,它会再次重新读取并继续

组合.txt:

adam
Student
william
Teacher
Joseph
Student
Hind 
Teacher
Raya 
Student
4

1 回答 1

2

您可以通过循环第一个文本文件的行并使用键上的模数插入文本文件#2 中的备用行来实现此目的。计算为list #2 key = the remainder of list #1 key divided by the number of lines in list #2,即$list2Key = $list1Key % $numberOfLinesInList2有关模数运算符的更多信息在这里。

$f1 = file('1.txt');
$f2 = file('2.txt');

$number_of_inserts = count($f2);

$output = array();
foreach ($f1 as $key => $line) {
    $output[] = $line;
    $output[] = $f2[$key % $number_of_inserts];
}

print_r($output);

这将适用于第二个文本文件中的任意数量的行。

于 2016-06-24T03:08:24.820 回答