3 # This program is in the public domain within the United States. Additionally,
4 # we waive copyright and related rights in the work worldwide through the CC0
5 # 1.0 Universal public domain dedication, which can be found at
6 # http://creativecommons.org/publicdomain/zero/1.0/
8 # These functions redutrn a csv download which is hopefully easy to import into
9 # Excell and LibreOffice Calc
11 # pass in a 2d array (array of rows) and it'll send it to the browser
12 # I can't figure out how to get multi-line fields to work, so this replaces newlines with 4 spaces.
13 function array2d_to_csv_download($data, $filename) {
14 header('Content-type: application/csv');
15 header('Content-Disposition: attachment; filename=' . $filename);
16 foreach($data as $row) {
18 foreach($row as $el) {
24 echo '"' . str_replace("\n", " ", str_replace('"', '""', $el)) . '"';
31 # pass an array of associative arrays. keys from the first one will be used
32 # see arary2d_to_csv_download above for more details
33 function assocs_to_csv_download($data, $filename) {
34 if(count($data) < 1) {
37 $flats = array(array_keys($data[0]));
39 foreach($data as $row) {
41 foreach($keys as $k) {
47 array2d_to_csv_download($flats, $filename);