Reading CSV File into PHP ARRAY

Using PHP to read CSV files
A Comma-separated values (CSV) file stores tabular data (numbers and text) in plain-text form. Often it is one record per line separated by a comma or any other delimiter. The comma , is often used in USA/UK CSV files where Germany often uses a ; instead

This is an example of a simple CSV file that has 4 fields and 2 records (1 per line)

In PHP it is often useful to be able to read a CSV file and access it’s data. That is where the fgetcsv() function comes in handy, it will read each line of a CSV file and assign each value into an ARRAY. You can define the separator in the function as well see PHP docs for fgetcsv() for more options and examples.

Here is a simple function that shows how to read our CSV file and return an ARRAY holding the data from the CSV.

Updated post : Feb 06, 2017

Leave a Reply

Your email address will not be published. Required fields are marked *