用php反复将N(6)行文本合并或合并到一个数组中 [英] Merge or combine N (6) lines of text in one array repeatedly with php
问题描述
需要从.txt文件准备数据,以通过浏览器导入数据库.在下面的示例中,有.txt文件(约40 000行).我需要将每6条记录合并到一个数组中(第一个始终是示例中的时间戳).因此,据我了解,有必要进行一些循环.试图找到一些例子(太简单了),但是没有成功.感谢您提供的一些帮助,甚至可以提供某些链接.
Need to prepare data from .txt file for importing in database via browser. In the example below, there is .txt file (about 40 000 lines). I need to combine every 6 records in one array (first is always timestamp in the example). So, as I understand, some loop is necessary. Tried to find some examples (too easy) but didn't succeed. I appreciate any help you can provide even some link.
.txt示例
1=19-10-18 10:02:06
2=+1.313026E+00 l/s
3=+1.671796E-01m/s
4=+1.500691E+02m3
5=+1.501138E+02m3
6=+0.000000E+00m3
1=19-10-18 10:03:06
2=+1.266786E+00 l/s
3=+1.612923E-01m/s
4=+1.501403E+02m3
5=+1.501850E+02m3
6=+0.000000E+00m3
1=19-10-18 10:04:06
2=+1.597391E+00 l/s
3=+2.033861E-01m/s
4=+1.502291E+02m3
5=+1.502738E+02m3
6=+0.000000E+00m3
看起来应该像这样:
array(6) {
[0]=>
string(15) "9-10-18 10:02:0"
[1]=>
string(17) "+1.313026E+00 l/s"
[2]=>
string(16) "+1.671796E-01m/s"
[3]=>
string(14) "+1.500691E+02m"
[4]=>
string(14) "+1.501138E+02m"
[5]=>
string(14) "+0.000000E+00m"
}
array(6) {
[0]=>
string(15) "9-10-18 10:03:0"
[1]=>
string(17) "+1.413026E+00 l/s"
[2]=>
string(16) "+1.771796E-01m/s"
[3]=>
string(14) "+1.300691E+02m"
[4]=>
string(14) "+0.501138E+02m"
[5]=>
string(14) "+1.000000E+00m"
}
.
.
.
我到目前为止所做的事情:
What i did till now:
$file=fopen("test_file/test.txt","r");
if ($fh = fopen('test.txt', 'r')) {
while (!feof($fh)) {
$line = fgets($fh);
$line=trim($line);
$line=trim($line,"1=");
$line=trim($line,"2=");
$line=trim($line,"3=");
$line=trim($line,"4=");
$line=trim($line,"5=");
$line=trim($line,"6=");
echo"<pre>";
echo $line;
}
fclose($fh);
}
所以现在的结果是:
9-10-18,10:02:0
+1.313026E+00 l/s
+1.671796E-01m/s
+1.500691E+02m
+1.501138E+02m
+0.000000E+00m
9-10-18,10:03:0
+1.266786E+00 l/s
+1.612923E-01m/s
+1.501403E+02m
+1.501850E+02m
+0.000000E+00m
现在我需要每6条记录放入一个数组.
Now I need to put in array every 6 records.
推荐答案
您可以使用 array_chunk($ array,6);
这篇关于用php反复将N(6)行文本合并或合并到一个数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!