Skip to content

Instantly share code, notes, and snippets.

@rewebcan
Created September 20, 2017 17:14
Show Gist options
  • Save rewebcan/c6b20613a53d67af755fc2659488cf73 to your computer and use it in GitHub Desktop.
Save rewebcan/c6b20613a53d67af755fc2659488cf73 to your computer and use it in GitHub Desktop.
PHP - String CSV to Associative Array
stringCSVToArray($csvString, $delimiter = ";", $newLine = "/\r\n/"){
if($csvString) {
$split = preg_split($newLine, $csvString);
$headRow = array_shift($split);
$headColumns = explode($delimiter, $headRow);
$array = [];
foreach ($split as $row) {
$rowContent = explode($delimiter, $row);
if(count($rowContent) == count($headColumns)){
$array[] = array_combine($headColumns, $rowContent);
} else {
throw new \Exception('Sayilar esit olmali');
}
}
return $array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment