php 跳过php中fgetcsv方法的第一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14372730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:09:07  来源:igfitidea点击:

skip first line of fgetcsv method in php

php

提问by please delete me

I have a csv file and i read data from csv file then i want to skip first line of csv file.Which'll contain any header. I am using this code.

我有一个 csv 文件,我从 csv 文件中读取数据,然后我想跳过 csv 文件的第一行。其中将包含任何标题。我正在使用此代码。

while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
    {
     $sql = "INSERT into da1(contact_first, contact_last, contact_email) values('$emapData[0]','$emapData[1]','$emapData[2]')";
       mysql_query($sql);

    }

When i insert data into th database then header should not to be saved into the database.

当我将数据插入数据库时​​,不应将标题保存到数据库中。

回答by Dustin Currie

Before beginning the while loop, just get the first line and do nothing with it. This way the logic to test if it's the first line is not needed.

在开始 while 循环之前,只需获取第一行并对其进行任何操作。这样就不需要测试它是否是第一行的逻辑。

fgetcsv($file, 10000, ",");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
  //....
}

回答by Sudhir Bastakoti

try:

尝试:

$flag = true;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
   if($flag) { $flag = false; continue; }
   // rest of your code
}

回答by uncovery

You can add a simple check and skip the query if the check fails:

如果检查失败,您可以添加一个简单的检查并跳过查询:

$firstline = true;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
if (!$firstline) {
     $sql = "INSERT into da1(contact_first, contact_last, contact_email) values('$emapData[0]','$emapData[1]','$emapData[2]')";
       mysql_query($sql);
    }
    $firstline = false;
}

回答by AdrienW

A bit late, but here is another way to do so (without having to count all the lines): with fgets

有点晚了,但这是另一种方法(无需计算所有行):使用fgets

$file = fopen($filename, 'r');  // create handler
fgets($file);  // read one line for nothing (skip header)
while (($line = fgetcsv($file, 10000, ",")) !== FALSE) {
    // do your thing
}

One might consider this to be more elegant

人们可能会认为这更优雅

回答by leonardogeranio

You should use the fseek() methodin order to get the desired line, regardless the current pointer position.

无论当前指针位置如何,您都应该使用fseek() 方法来获取所需的行。

At this example, get the first line after the loop:

在此示例中,获取循环后的第一行:

$file = fopen($path, "r");    

while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
  // ...
}

fseek($file, 1, SEEK_CUR);

You can use the third parameter to position the pointer in the file, as is:

您可以使用第三个参数在文件中定位指针,如下所示:

SEEK_SET – It moves file pointer position to the beginning of the file.

SEEK_SET - 它将文件指针位置移动到文件的开头。

SEEK_CUR – It moves file pointer position to given location.

SEEK_CUR – 将文件指针位置移动到给定位置。

SEEK_END – It moves file pointer position to the end of file.

SEEK_END – 将文件指针位置移动到文件末尾。

回答by Amarendra Kumar

Try this simple code.

试试这个简单的代码。

$file = fopen('example.csv', 'r');  // Here example is a CSV name
$row = 1;
while (($line = fgetcsv($file, 10000, ",")) !== FALSE) {
// $line is an array of the csv elements
if($row == 1){ $row++; continue; }   // continue is used for skip row 1
// print_r($line);
// rest of your code
}

Hope its work..Cheers..

希望它的工作..干杯..