Last active
August 29, 2015 14:05
-
-
Save Penderis/f18c94c8e0e8dcb728a2 to your computer and use it in GitHub Desktop.
Create CSV from Associative Array - PHP
This file contains hidden or 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
$header=null; | |
//header nulled first line will make not nulled thus creating csv with header values | |
$pathToGenerate = "path to new file location including filename"; | |
//create directory if does not exist beforehand, file will be created or override in fopen with w+ flag | |
$createFile = fopen($pathToGenerate,"w+"); | |
foreach ($assocArray as $row) { | |
if(!$header){ | |
//this matches the first line to create header values | |
fputcsv($createFile,array_keys($row)); | |
}else{ | |
//this will fill information per row | |
fputcsv($createFile, $row); | |
} | |
} | |
//this closes the file | |
fclose($createFile); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment