我有一个列表(data1.php),其中包含如下数据:
06/26/2015,33.51718,-81.71856,0.0,4001,15:39:57,39256,1,0.0,13.6,
06/26/2015,33.51721,-81.71858,0.0,4001,15:40:57,39256,1,0.0,13.7,
06/26/2015,33.51720,-81.71860,0.0,4001,15:41:57,39256,1,0.0,13.6,
06/26/2015,33.51719,-81.71858,0.0,4001,15:42:58,39256,1,0.0,13.7,
06/26/2015,33.51720,-81.71860,0.0,4001,15:43:58,39256,1,0.0,13.6,
我正在尝试仅检索第2和第3个数据(lat和long)。输出文件的结果如下:
Lat:33.51718
Long:-81.71856
Lat:33.51720
Long:-81.71860
Lat:33.51720
Long:-81.71860
它正在跳过其他所有行。这是我的代码。请帮忙。
<?php
$fn = fopen("data1.php",'r') or die("fail to open file");
$fp = fopen('output.php', 'w') or die('fail to open output file');
while($row = fgets($fn)) {
echo fgets($fn). "<br";
$num = explode(",", $row);
$lat = $num[1];
$long = $num[2];
echo "<p>Lat: {$lat}</p>";
echo "<p>Long: {$long}</p>";
fwrite($fp, "Lat:$lat\n");
fwrite($fp, "Long:$long\n");
}
fclose($fn);
fclose($fp);
?>
答案 0 :(得分:1)
你的问题就在这里:
while($row = fgets($fn)) {
echo fgets($fn). "<br";
您从文件中读取1行,然后立即读取并回显另一行而不实际处理它。删除回声,你应该没事。
答案 1 :(得分:0)
试试这个:
<?php
$fn = fopen("data1.php",'r') or die("fail to open file");
$fp = fopen('output.php', 'w') or die('fail to open output file');
$stop = "0";
while($row = fgets($fn)) {
$num = explode(",", $row);
$lat = $num[1];
$long = $num[2];
if($stop == "1" or $stop == "2"){
echo "<p>Lat: {$lat}</p>";
echo "<p>Long: {$long}</p>";
fwrite($fp, "Lat:$lat\n");
fwrite($fp, "Long:$long\n");
}else{}
$stop++;
}
fclose($fn);
fclose($fp);
?>