how do i parse a csv file to grab the column names first then the rows that relate to it?

For reading it all at once you can use:

$csv = array_map("str_getcsv", file("file1.csv",FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);

To turn all the rows into a nice associative array you could then apply:

foreach ($csv as $i=>$row) {
    $csv[$i] = array_combine($keys, $row);
}

Leave a Comment