跳过CSV文件的第一行 [英] Skip the first line of a CSV file

查看:1032
本文介绍了跳过CSV文件的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试汇入CSV档案。由于我们使用的程序,第一行基本上是所有的头,我想跳过,因为我已经通过HTML自己的头。如何获取代码跳过CSV的第一行? (strpos命令用于截断所有行中的第一个字段。)

I am trying to import a CSV file. Due to the program we use, the first row is basically all headers that I would like to skip since I've already put my own headers in via HTML. How can I get the code to skip the first row of the CSV? (the strpos command is to cut off the first field in all the rows.)

<?php
$row = 1;
if (($handle = fopen("ptt.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
           $row++;
    for ($c=0; $c < $num; $c++) {
    if(strpos($data[$c], 'Finished') !== false) {
    $c++;
echo "<TR> <TD nowrap>" . $data[$c] . "</ TD>"; }
    Else{
        echo "<TD nowrap>" .  $data[$c] . "</ TD>";
        }
    }
}
fclose($handle);
}
?>


推荐答案

您可以使用 continue 跳过第一行的其余循环。

As you are keeping track of the row number anyway, you can use continue to skip the rest of the loop for the first row.

例如,将( $ num = count($ data))的开头:

if($row == 1){ $row++; continue; }

还有其他方法可以做到这一点,但只要确保当你继续, $ row 仍在递增,否则会产生无限循环!

There are other ways to do this, but just make sure that when you continue, $row is still being incremented or you'll get an infinite loop!

这篇关于跳过CSV文件的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆