How do I get the second line from this CSV file?
<?php
$file = file('list.csv');
while (($line = fgetcsv($file)) !== FALSE) {
print_r($line);
}
?>
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++
}
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);