I have a CSV file with an initial header row and an unknown number of rows. The row format is:
name_data, email_data, cell_data, dob_data
I'd like to open the CSV file and then depict the data from the last entered row in a table, like this:
Name: name_data
Email: email_data
Cell: cell_data
D.O.B.: dob_data
I'm thinking I can use fgetcsv() but I'm not sure how to parse the data once I get it.
Any ideas?
Thanks - Joe
Seems inefficient to parse every line of the file with fgetcsv()
when you only care about the first and last line. As such, I'd use file()
and str_getcsv()
:
$rows = file('data.csv');
$last_row = array_pop($rows);
$data = str_getcsv($last_row);
// loop and output data
Note: You can use the same logic for the headers by parsing $rows[0]
.