Created
September 17, 2019 08:41
-
-
Save pavel-one/d6b28db5929c82c47ecc53f11e6cb17f to your computer and use it in GitHub Desktop.
Csv2Array
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 | |
/** | |
* 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