How To Convert A CSV File Into An Array In PHP

I had a CSV (comma separated value) file that listed all the suburbs in Australia, along with their postcode, latitude and longitude. I needed it saved in a file as an array for a project I'm doing, but didn't want to have to run multiple regex queries, especially if I needed to use it on other location groups.

How do you convert a csv file into an array in php? Use array_map with str_getcsv to pull the csv file into the array. Then use a foreach loop to iterate over each item, echoing the elements in the format you need.

Let's see how that looks in code:

$csv = array_map('str_getcsv', file('locations.csv'));

echo "array (<br>";
foreach($csv as $location => $data)
  echo " array (<br>";
  echo '  "postcode" => "' . $data[1] . '",<br>';
  echo '  "name" => "' . $data[2] . '",<br>';
  echo '  "state_code" => "' . $data[3] . '",<br>';
  echo '  "lat" => "' . $data[4] . '",<br>';
  echo '  "lng" => "' . $data[5] . '"<br>';
  echo " ),<br>";
echo ");";

That produced an array that looked like this:

array (
 array (
  "postcode" => "2850",
  "name" => "Aarons Pass",
  "state_code" => "NSW",
  "lat" => "-32.86328",
  "lng" => "149.80375"
 array (
  "postcode" => "6280",
  "name" => "Abba River",
  "state_code" => "WA",
  "lat" => "-33.68488",
  "lng" => "115.46334"
 array (
  "postcode" => "6280",
  "name" => "Abbey",
  "state_code" => "WA",
  "lat" => "-33.66077",
  "lng" => "115.25863"

I then copied and pasted the outputted code into my project, added $locations = before it and it was all done.

How Does It Work?

All the magic is in the first line.

$csv = array_map('str_getcsv', file('locations.csv'));

I got that line from the PHP Manual for str_getcsv. You can use it whenever you need to turn a csv file into an array.

The line turns each line of the csv file into a regular array (ie $item[0], $item[1], etc), so you end up with an array of arrays.

Be careful doing this with a file you don't control. It can lead to some pretty serious security risks.

I needed an array of associated arrays, which is why I did the foreach loop.

I also needed it printed out so I could save the whole thing to a file for later use, hence the echoing.

I actually did it slightly different from the code here. I didn't have all the spaces and <br>. I put that in for this tutorial because it creates each associated array on the same line and is harder to read online.

4 thoughts on “How To Convert A CSV File Into An Array In PHP”

  1. Hi Mike, did the CSV have column headings? I'm asking because you typed in the column heads in your echo statements. I'm about to try your code to see if I can cut down on the number of lines I'm using in a function I found elsewhere.


    • Hey Mark

      The CSV I used didn't have column headings. If yours does and you want to map the column headings, you might pull $csv[0] into a separate array (for code clarity) and then pull those elements into the echo statements.

      You'd either need to know how many columns there were, or send the echoing off to another function to avoid nesting foreach statements.

      In the main foreach, you would also do an
      if ($location == 0) {continue;}
      to skip the headings row.


Leave a Comment