Asked  1 Year ago    Answers:  5   Viewed   13 times

I am trying to import a CSV file. Due to the program we use, the first row is basically all headers that I would like to skip since I've already put my own headers in via HTML. How can I get the code to skip the first row of the CSV? (the strpos command is to cut off the first field in all the rows.)

$row = 1;
if (($handle = fopen("ptt.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    for ($c=0; $c < $num; $c++) {
    if(strpos($data[$c], 'Finished') !== false) {
echo "<TR> <TD nowrap>" . $data[$c] . "</ TD>"; }
        echo "<TD nowrap>" .  $data[$c] . "</ TD>";



As you are keeping track of the row number anyway, you can use continue to skip the rest of the loop for the first row.

For example, add this at the start of your while loop (just above $num = count($data)):

if($row == 1){ $row++; continue; }

There are other ways to do this, but just make sure that when you continue, $row is still being incremented or you'll get an infinite loop!

Thursday, April 1, 2021

Like you said in your title, fgetcsv is the way to go. It's pretty darn easy to use.

$file = fopen('myCSVFile.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
  //$line is an array of the csv elements

You'll want to put more error checking in there in case fopen() fails, but this works to read a CSV file line by line and parse the line into an array.

Saturday, June 5, 2021

You can delete the first line of a file using sed:

sed -i '' 1d file.csv

If you just want to get the contents of the file without the first line, and without modifying the file, remove the -i '' flag:

sed 1d file.csv

Here 1d is the command to execute:

  • 1 => line where to act
  • d => delete

So 1d means 'delete line 1'

If you want to get the first line, you can use sed too:

sed -n 1p file.csv

Here p stands for 'print' or

sed 1q file.csv

(see William Pursell's comment)

Sunday, August 8, 2021

Building on Nouveau's code you could do this

for($i = 0; $i <= count($fieldList); $i++)
    if (is_numeric($fieldList[$i]))
        if (strpos($fieldList[$i],'.') !== false){
            $fieldList[$i] = (int)$fieldList[$i];
            $fieldList[$i] = (float)$fieldList[$i];

    switch(gettype($fieldList[$i])) {
        case 'integer':
            $typeInfo = 'int(11)';
        case 'float':
        case 'double':
            $typeInfo = 'float';

        case 'string':
            $typeInfo = 'varchar(80)';
            $typeInfo = 'varchar(80)';
if(gettype($fieldList[$i]) != NULL) echo "t".'`'.$i.'` '.$typeInfo.' NOT NULL, --'.gettype($fieldList[$i]).' '.$fieldList[$i]."<br/>n";


That works, note the addition of "case 'double':" in the switch, but there may be a better way to do the int/float check as they would only work with standard uk/us numbers.

Friday, August 13, 2021

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.

Tuesday, August 17, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :