+
+# pass an array of associative arrays. keys from the first one will be used
+# see arary2d_to_csv_download above for more details
+function assocs_to_csv_download($data, $filename) {
+ if(count($data) < 1) {
+ $flats = array();
+ } else {
+ $flats = array(array_keys($data[0]));
+ $keys =& $flats[0];
+ foreach($data as $row) {
+ $flat = array();
+ foreach($keys as $k) {
+ $flat[] = $row[$k];
+ }
+ $flats[] = $flat;
+ }
+ }
+ array2d_to_csv_download($flats, $filename);
+}