如何使用 fgetcsv 在 while 循环中跳过标题行?
我无法像我之前使用的代码那样获得我编写的跳过第一行(标题)的新代码(见底部)。
我没有收到任何错误,但只是无法让它省略第一行。
$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);
以下是跳过标题的旧源,供参考和比较。
$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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么还要数呢?只需在循环之前获取标题即可。
另外,您只是将文件指针分配给变量。
Why even count? Just get the headers before looping.
Also, you're only assigning the file pointer to the variable.
当
$row_count
为0
时,您没有读取任何行。更改
为
When
$row_count
is0
you are not reading any row.Change
to