Skip to content

Latest commit

 

History

History
21 lines (18 loc) · 454 Bytes

read-csv-file-in-php.md

File metadata and controls

21 lines (18 loc) · 454 Bytes

How to read a CSV file in PHP

<?php

$file = 'data.csv';
$separator = "\t";
$done = 0;
if (($handle = fopen($file, "r")) !== FALSE) {
   while (($data = fgetcsv($handle, false, "$separator")) !== FALSE) {
    $done += 1;
    if ($done < 2) {
      $fields = explode($separator, str_replace(' ', '', implode($separator, $data)));
      continue;
    }

    $item = array_combine($fields, $data);
     print_r($item);
  }
}