Website development and design blog, tutorials and inspiration

PHP Function to Import CSV to an Array

Import a CSV into a PHP array with this function

Written By on in PHP 4

PHP Function to Import CSV to an Array

359 words, estimated reading time 2 minutes.

How to import CSV file into a PHP array using PHP's fgetcsv function. The array is associative so you can access values by the column heading from the CSV.

This PHP function will import CSV and convert it to an associative array based on the column headings in the first row. To use, simply call the function with a valid filename as the only parameter. The function will return an associative array of the import CSV file contents.

The import CSV must have column headings as the first row since these are used to create the associative array elements.

Import CSV to Array with PHP

  1. <?php
  3. function ImportCSV2Array($filename)
  4. {
  5. $row = 0;
  6. $col = 0;
  8. $handle = @fopen($filename, "r");
  9. if ($handle)
  10. {
  11. while (($row = fgetcsv($handle, 4096)) !== false)
  12. {
  13. if (empty($fields))
  14. {
  15. $fields = $row;
  16. continue;
  17. }
  19. foreach ($row as $k=>$value)
  20. {
  21. $results[$col][$fields[$k]] = $value;
  22. }
  23. $col++;
  24. unset($row);
  25. }
  26. if (!feof($handle))
  27. {
  28. echo "Error: unexpected fgets() failn";
  29. }
  30. fclose($handle);
  31. }
  33. return $results;
  34. }

Example usage

  1. <?php
  3. $filename = "test.csv";
  4. $csvArray = ImportCSV2Array($filename)
  6. foreach ($csvArray as $row)
  7. {
  8. echo $row['column1'];
  9. echo $row['column2'];
  10. echo $row['column3'];
  11. }

What is happening?

This function takes each line of the CSV import file and runs it through PHP's fgetcsv function. This function is similar to fgets except that fgetcsv parses the line it reads for fields in CSV format and returns an array containing the fields read. The array is an indexed array, with the first item at 0, second at 1 and so on.

To get the associative array, the first row of data is used as the column headings, then a loop runs over each item in the array from fgetcsv and inserts it against the column heading.

The method of using associative arrays is far better than an indexed array, as it can often be confusing, cause errors trying to remember what piece of data is at any given numerical index, and code maintenance is often a minefield. If a new column is added to the data, you have to update all the indexes where that array is used. With an associative array, you access data via a name, so it does not matter what order the data is read in or how many columns are inserted, that name will remain constant.

Last updated on: Tuesday 20th June 2017

Did you Like this Post? Why not Like us on Facebook?


  1. Gary

    Hi, great function! How would you start reading at say the 300th row? I'm trying to work this out but am not an expert in PHP, can it be done within your function? Thanks in advance!

    1. Tim Trott
      Tim Trott

      Just a quick idea, you could check the counter before processing the row. Something similar to this should work:

      while (($row = fgetcsv($handle, 4096)) !== false)
      if (empty($fields))
      $fields = $row;

      // start at row 300. $col is 0 indexed
      if ($col > 299)
      foreach ($row as $k=>$value)
      $results[$col][$fields[$k]] = $value;


  2. Nick

    Many thanks, you saved the day.

  3. Steve

    Thanks Tim, used it today, works great!

Leave a Reply

Your email address will not be published.