Skip to content

Instantly share code, notes, and snippets.

@ooooak

ooooak/read-csv.php

Created Feb 16, 2018
Embed
What would you like to do?
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