Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Created March 3, 2014 00:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tegansnyder/9315965 to your computer and use it in GitHub Desktop.
Save tegansnyder/9315965 to your computer and use it in GitHub Desktop.
Write a associative array to a CSV file.
<?php
$arr = array();
$arr[0]['derp'] = 'hi';
$arr[0]['blerg'] = 'hey';
produceCSV('data.csv', $arr);
function produceCSV($file_name, $arr) {
$has_header = false;
foreach ($arr as $c) {
$fp = fopen($file_name, 'a');
if (!$has_header) {
fputcsv($fp, array_keys($c));
$has_header = true;
}
fputcsv($fp, $c);
fclose($fp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment