Skip to content

Instantly share code, notes, and snippets.

@devuri
Last active October 17, 2017 16:12
Show Gist options
  • Select an option

  • Save devuri/887f609380e76117ca3f797f2f2aa4f8 to your computer and use it in GitHub Desktop.

Select an option

Save devuri/887f609380e76117ca3f797f2f2aa4f8 to your computer and use it in GitHub Desktop.
Read CSV to ARRAY
<?php
function get_csvarray($getcsvfile = 'test.csv'){
$row = 0;
$col = 0;
$gethandle = @fopen($getcsvfile, "r");
if ($gethandle) {
while (($row = fgetcsv($gethandle, 4096)) !== false) {
if (empty($fields)) {
$fields = $row;
continue;
}
foreach ($row as $k=>$value) {
$outputarray[$col][$fields[$k]] = $value;
}
$col++;
unset($row);
}
if (!feof($gethandle)) {
echo "Error: unexpected fgets() failn";
}
fclose($gethandle);
}
return $outputarray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment