csv to associative array

i’ve been working a major integration project with quickbase and vtrenz. one of the things i had to do this week was take a ~10K row csv, scrub the cells for specific length, and then ftp to a remote server.

so i wrote a php script that would connect to quickbase, pull down the csv, write it to file, open it, turn the csv into an array, scrub it, recreate the csv from the array, write it to file again, and then send it on it’s merry way ala ftp.

all of it was easy except turning the csv into an array. i didn’t want to step through each character looking for newlines or my delimeter (a comma) and i needed something that would use the first row of the csv as the column headings.

i was able to find some scripts on the web that half-way worked. in fact, the one i used before writing my own worked, it was just super slow and inefficient. it literally used a gig of memory for 5K records.

so here’s my solution for taking a very large csv, and using the first row to create a key=>value associative array.

function buildStock($File) {
  $handle = fopen($File, "r");
  $fields = fgetcsv($handle, 5000, ",");

  $y = 0;
  while($data = fgetcsv($handle, 5000, ",")) {
    $x = 0;
    foreach($data as $value) {
      $stock[$y][$fields[$x]] = $value;
        $x++;
     }
     $y++;
    }
   return $stock;
}

it now only takes a few seconds to turn a considerably large csv into an array. hope this helps someone out there.

Published byJosh McGinnis

Josh is a software engineer, leader, startup advisor for the LA Chamber of Commerce and consultant residing in the Los Angeles area. Josh is passionate about helping entrepreneurs and businesses reach their maximum potential.

5 Comments

  • Daniel

    May 23, 2010 at 8:11 am Reply

    Can you explain your function a little? What does $rID refer to? What sort of data does the $rID consist of? Where is $rID applied in the function?

    • Craig

      July 29, 2010 at 5:30 pm Reply

      $rID does nothing in that code. It’s passed into the function but then never gets referenced. You should just remove it.

      • Joshua McGinnis

        July 30, 2010 at 1:56 pm Reply

        Good catch. For the site I work on, and for the reason I use this function, $rID is required – but for the purposes of this example, it isn’t necessary.

  • Daniel

    June 22, 2011 at 4:51 pm Reply

    Thanks! Worked perfectly!

  • […] appears in the Laravel documentation). After a quick search, I found Joshua McGinnis’s post csv to associative array, and it’s just what I needed. I (very) slightly adapted his code and turned it into a […]

Post a Comment