Skip to content

Instantly share code, notes, and snippets.

@Penderis
Last active August 29, 2015 14:05
Show Gist options
  • Save Penderis/f18c94c8e0e8dcb728a2 to your computer and use it in GitHub Desktop.
Save Penderis/f18c94c8e0e8dcb728a2 to your computer and use it in GitHub Desktop.
Create CSV from Associative Array - PHP
$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