0

I have this readCSV function that for now skips the first line, but I need it to skip the three first lines.

What is the preferred way of achieving that?

function readCSV($csvFile){
   $file_handle = fopen($csvFile, 'r');
   while (!feof($file_handle) ) {
     $array = fgetcsv($file_handle, 'r', ';');
     $line_of_text[] = array('dato'=>$array[0],'vs'=>trim($array[1]),'vf'=>trim($array[2]));
   }
   fclose($file_handle);
   return $line_of_text;
 }

$csvFile = 'http://some.file.csv';
Filip Blaauw
  • 731
  • 2
  • 16
  • 29

2 Answers2

0

Use a counter to keep track of home many lines have been processed:

  function readCSV($csvFile){
   $file_handle = fopen($csvFile, 'r');
   $counter = 0;
   while (!feof($file_handle) ) {
    if($counter < 3){
         $array = fgetcsv($file_handle, 'r', ';');
         $line_of_text[] = array('dato'=>$array[0],'vs'=>trim($array[1]),'vf'=>trim($array[2]));
    }
    $counter++
   }
   fclose($file_handle);
   return $line_of_text;
 }
0

Keep a counter to count lines processed inside the loop, act only once the counter is greater than 3.

Example:

function readCSV($csvFile){

  $counter = 0;

  $file_handle = fopen($csvFile, 'r');

  while (!feof($file_handle) ) {
    if($counter > 3){
      $array = fgetcsv($file_handle, 'r', ';');
      $line_of_text[] = array('dato'=>$array[0],'vs'=>trim($array[1]),'vf'=>trim($array[2]));
    }
    $counter++;
  }

  fclose($file_handle);
  return $line_of_text;
}

$csvFile = 'http://some.file.csv';
coderodour
  • 1,072
  • 8
  • 16