如何使标题行在我的while循环使用fgetcsv跳过? [英] How to make the header row be skipped in my while loop using fgetcsv?

查看:147
本文介绍了如何使标题行在我的while循环使用fgetcsv跳过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获取我写的新代码跳过第一行(标题)作为我之前使用的代码(见下)。

I can't get the new code I've written to skip the first row (header) as the code I was using before did (see bottom).

我没有收到任何错误,但无法让它省略第一行。

I'm not receiving any errors, but just can't get it to omit first row.

$file = fopen($uploadcsv,"r");
$column_headers = array();
$row_count = 0;
while(!feof($file)) {   
  if ($row_count==0){
    $column_headers = $file;
  } else {
    print_r(fgetcsv($file));
  }
  ++$row_count;
  }

fclose($file);

下面是跳过标题的旧源,供参考和比较。

Below is the old source that skipped the header, for reference and comparison.

$handle = fopen($uploadcsv, 'r');
$column_headers = array();
$row_count = 0;
while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) {
  if ($row_count==0){
    $column_headers = $data;
  } else {
    print_r($data);
  }
  ++$row_count;
}
fclose($handle);


推荐答案

只需在循环之前获取标头。

Why even count? Just get the headers before looping.

$column_headers = fgetcsv($file);
while(!feof($file)) {
   ...

此外,您只需将文件指针分配给变量。

Also, you're only assigning the file pointer to the variable.

这篇关于如何使标题行在我的while循环使用fgetcsv跳过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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