I am having troubles reading a CSV file. So far, I am able to open the CSV and read the first line.
My CSV file looks like this (notice each new line):
SMITH
PHIL
CHARLIE
SALLY
ETC
I save the file in excel as a CSV, but it is getting caught up on each line break.
My current code looks like this:
<?php
$file = fopen('surnames.csv', 'r');
$data = fgetcsv($file);
echo "<pre>";
print_r($data);
echo "</pre>";
fclose($file);
?>
Current output is
Array
(
[0] => SMITH
)
Any help is appreciated. My anticipated output is obviously a single array with all of the values.
Thank you.
You have to loop over each line to read the whole file, otherwise you’re only reading the first line in the file:
Now
$datawill be a multidimensional array with every line from the CSV file.