Pierstoval
7/29/2015 - 7:41 AM

Transform CSV file into PHP associated array

Transform CSV file into PHP associated array

<?php

$csvFile = 'csv_file.csv';

$fileHandle = fopen($csvFile, 'r');

// Get the fields from the first row
$fields = fgetcsv($fileHandle, 3000, ';', '"');

$elements = array();

while ($row = fgetcsv($fileHandle, 1000, ';', '"')) {
  $element = array_combine($fields, $row);
  // $element is an associated array
}

fclose($fileHandle);