Created
February 16, 2018 12:56
-
-
Save ooooak/6615a820d777c0534b76d1fb9e377bea to your computer and use it in GitHub Desktop.
read csv in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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