How to skip first line of csv file

Asked

Viewed 857 times

0

I need to ignore the first line of a file I’m importing in csv with php, this first line is a header, some hint how can I do this? I tried to make an accountant, but I couldn’t fix it, what I have is this:

if (!empty($_FILES)) {

    $tempFile = $_FILES['userImage']['tmp_name'];
    $Dados = file($tempFile);

    foreach($Dados as $RegLinhas) {

        // RETIRA ESPAÇOS NO INÍCIO E FIM
        $Linha = trim($RegLinhas);
        // DIVIDE A STRING
        $CamposImport = explode(';', $Linha);
        $IdPessoa = $CamposImport[0];       
        $Nome = $CamposImport[1];
        $IdUnicoop = $CamposImport[2];
        $DataAdmissao = $CamposImport[3];
        $IdCargo = $CamposImport[4];
        $LotacaoRH = $CamposImport[5];
        $CTPS = $CamposImport[6];
        $CTPSSerie = $CamposImport[7];
        $CTPSUF = $CamposImport[8];
        $DataNascimento = $CamposImport[9];
        $CPF = $CamposImport[11];

    }           
}

1 answer

0


You can do the following to remove the first array item, array_shift():

...
$Dados = file($tempFile);
array_shift($Dados); // remover primeira linha, primeiro elemento do array
...

DEMONSTRATION

Although the above way is the simplest/best, here is a way without using function:

...
foreach($Dados as $key => $RegLinhas) {
    if($key == 0) continue; // saltar para a prox volta caso seja a primeira linha
    ...
}

DEMONSTRATION

  • Hello @Miguel, spectacular tip, thank you very much, both ways worked.

  • @adventistapr still well. Good luck

Browser other questions tagged

You are not signed in. Login or sign up in order to post.