PHP动态创建CSV : Skip the first line of a CSV file

标签 php html csv fgetcsv

我正在尝试导入 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/

相关文章:

php - Drupal:子主题 CSS 不起作用

php - 如何使用 cakephp 1.3 上传文件

php - Laravel背包关系疑惑

javascript - 如何动态显示 div 而无需先将其加载到页面上?

pandas - 如何在 Jupyter 中打开 .tsv 文件? Jupyter.Notebook 尝试了建议,但它不起作用

php - 显示数据库中的 1 张以上图像? (PHP、MySQL)

html - UnityScript 音频问题

带分隔符的 PHP CSV 导入 - 忽略逗号?

python - 在 python 中按定义的间隔按 id/group 移动平均值

javascript - 将 jquery 代码从定位值更改为链接