我正在尝试导入 CSV 文件。由于我们使用的程序,第一行基本上是我想跳过的所有标题,因为我已经通过 HTML 放入了自己的标题。如何让代码跳过 CSV 的第一行? (strpos命令就是把所有行的第一个字段截掉。)
<?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);
}
?>
最佳答案
与其使用 if 条件来检查它是否是第一行,更好的解决方案是在 while 循环开始的行之前添加额外的代码行,如下所示:
....
.....
fgetcsv($handle);//Adding this line will skip the reading of th first line from the csv file and the reading process will begin from the second line onwards
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
.......
.......
就这么简单......
关于PHP动态创建CSV : Skip the first line of a CSV file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10901113/