Created
September 20, 2017 17:14
-
-
Save rewebcan/c6b20613a53d67af755fc2659488cf73 to your computer and use it in GitHub Desktop.
PHP - String CSV to Associative Array
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
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