Skip to content

Instantly share code, notes, and snippets.

@rewebcan
rewebcan / gist:c6b20613a53d67af755fc2659488cf73
Created September 20, 2017 17:14
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);