0

How do I get the second line from this CSV file?

 <?php

        $file = file('list.csv');
        while (($line = fgetcsv($file)) !== FALSE) {
            print_r($line);  
        }

 ?>
vinoli
  • 457
  • 1
  • 6
  • 20

2 Answers2

2

You can use an iterator to apply condition

 $file = file('list.csv');
 $i = 0;
 while (($line = fgetcsv($file)) !== FALSE) {
    if($i == 1){
      print_r($line);
      break;
    }
    $i++      
 }
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

Firstly file() reads the file into an array, so you don't need that - just open it using fopen() (and don't forget to close it), then if you only want the second line, then just call fgetcsv() twice...

$file = fopen('list.csv', r);
$line = '';
if (fgetcsv($file) !== FALSE) {
    $line=fgetcsv($file);  
}
fclose($file);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55