Maison > Article > développement back-end > 我的从TXT中读取的矩阵,但是每一行最后一个值总跟下一行第一个值一起输出。怎么破?
新手请教各位大神: 我的矩阵每一行最后一个值总跟下一行第一个值一起输出。怎么破?jsl_mini.txt里是一个矩阵:
2015/5/4 4857.00 4780.00 4480.46
2015/5/5 4692.00 4736.00 4298.71
$file = 'jsl_mini.txt';
echo $file.'
';
$content = file_get_contents($file);
$array = explode("\t", $content);
echo $array[0].'
';
echo $array[1].'
';
echo $array[2].'
';
echo $array[3].'
';
echo $array[4].'
';
echo $array[5].'
';
?>
输出结果为:
jsl_mini.txt
2015/5/4
4857.00
4780.00
4480.46 2015/5/5
4692.00
4736.00
就是红色这一行,分开4480.46 和2015/5/5用什么?
建议这样写
$file = 'jsl_mini.txt';$rows = file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);foreach($rows as $content) { $array = explode("\t", $content); echo $array[0].'<br/>'; echo $array[1].'<br/>'; echo $array[2].'<br/>'; echo $array[3].'<br/>';}
<?php$data = <<<TXT2015/5/4 4857.00 4780.00 4480.462015/5/5 4692.00 4736.00 4298.71TXT;$d = explode("\r\n", $data);foreach($d as $k){ $t = explode("\t", $k); foreach($t as $t1){ echo $t1.'<br>'; }}?>
建议这样写
$file = 'jsl_mini.txt';$rows = file($file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);foreach($rows as $content) { $array = explode("\t", $content); echo $array[0].'<br/>'; echo $array[1].'<br/>'; echo $array[2].'<br/>'; echo $array[3].'<br/>';}
<?php$data = <<<TXT2015/5/4 4857.00 4780.00 4480.462015/5/5 4692.00 4736.00 4298.71TXT;$d = explode("\r\n", $data);foreach($d as $k){ $t = explode("\t", $k); foreach($t as $t1){ echo $t1.'<br>'; }}?>