Skip to content

Instantly share code, notes, and snippets.

@ooooak
Created February 16, 2018 12:56
Show Gist options
  • Save ooooak/6615a820d777c0534b76d1fb9e377bea to your computer and use it in GitHub Desktop.
Save ooooak/6615a820d777c0534b76d1fb9e377bea to your computer and use it in GitHub Desktop.
read csv in php
<?php
$path = '/tmp/test.csv';
$header = [];
$items = [];
foreach (array_map('str_getcsv', file($path)) as $key => $row) {
if ($key == 0){
$header = $row;
}else{
try {
$items[] = array_combine($header, $row);
} catch (Exception $e) {
echo $e->getMessage();
exit(PHP_EOL . __FILE__ . ': ' . __LINE__ . PHP_EOL);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment