Skip to content

Instantly share code, notes, and snippets.

@pavel-one
Created September 17, 2019 08:41
Show Gist options
  • Save pavel-one/d6b28db5929c82c47ecc53f11e6cb17f to your computer and use it in GitHub Desktop.
Save pavel-one/d6b28db5929c82c47ecc53f11e6cb17f to your computer and use it in GitHub Desktop.
Csv2Array
<?php
/**
* parse_csv
* parse CSV file into big array
*
* @param string $file
* @param string $separator
* @param array $meta
* @return array
*/
function parse_csv($file, $separator=';', $meta=array()) {
$count = 0;
$data = array();
if (($fp = fopen($file, 'r')) !== false ) {
while (($row = fgetcsv($fp, 1024, $separator)) !== false) {
if ($count === 0 && empty($meta) && strpos($row, '#') !== false) {
$meta = $row;
$meta = array_map(create_function('$s', 'return str_replace("#", "", $s);'), $meta);
}
$data[] = empty($meta) ? $row : array_combine($meta, $row);
$count++;
}
fclose($fp);
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment