php将array变成csv格式

 1 function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
 2     // open raw memory as file so no temp files needed, you might run out of memory though
 3     $f = fopen('php://memory', 'w'); 
 4     // loop over the input array
 5     foreach ($array as $line) { 
 6         // generate csv lines from the inner arrays
 7         fputcsv($f, $line, $delimiter); 
 8     }
 9     // rewrind the "file" with the csv lines
10     fseek($f, 0);
11     // tell the browser it's going to be a csv file
12     header('Content-Type: application/csv');
13     // tell the browser we want to save it instead of displaying it
14     header('Content-Disposition: attachement; filename="'.$filename.'";');
15     // make php send the generated csv lines to the browser
16     fpassthru($f);
17 }

 

 1         $f = fopen('php://memory', 'w'); 
 2 //        $lca=implode("\r\n",$new_rcarray);
 3 //        fwrite($f,$lca);
 4 //        fseek($f, 0);
 5         foreach ($new_rcarray as $v){
 6             fwrite($f,$v."\r\n");
 7         }
 8         fseek($f, 0);
 9         
10         header("Content-type:text/csv");
11         header("Content-Type: application/force-download");
12         header("Content-Disposition: attachment; filename=local_calling_area.txt");
13         header('Expires:0');
14         header('Pragma:public');
15         //echo "\xFF\xFE".mb_convert_encoding($lca,'UCS-2LE','UTF-8');
16         fpassthru($f);

 

posted @ 2014-07-29 15:31  haishi633  阅读(293)  评论(0编辑  收藏  举报