Home >Backend Development >PHP Tutorial >我的从TXT中读取的矩阵,但是每一行最后一个值总跟下一行第一个值一起输出。怎么破?

我的从TXT中读取的矩阵,但是每一行最后一个值总跟下一行第一个值一起输出。怎么破?

WBOY
WBOYOriginal
2016-06-23 13:24:07911browse


新手请教各位大神: 我的矩阵每一行最后一个值总跟下一行第一个值一起输出。怎么破?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/>';}

FILE_SKIP_EMPTY_LINES 跳过空行
FILE_IGNORE_NEW_LINES 去掉换行符

<?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>';    }}?>


2015/5/4
4857.00
4780.00
4480.46
2015/5/5
4692.00
4736.00
4298.71

建议这样写

$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/>';}

FILE_SKIP_EMPTY_LINES 跳过空行
FILE_IGNORE_NEW_LINES 去掉换行符


谢谢您!

<?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>';    }}?>


2015/5/4
4857.00
4780.00
4480.46
2015/5/5
4692.00
4736.00
4298.71



谢谢您!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php 实现反射Next article:php?去掉空格