企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
``` <?php #导出功能 $data = [ [ 'date' => '2019-06-21', 'client_code' => 'C0282', 'client_name' => '国洋测试', 'action' => '接口名字' ], [ 'date' => '2019-06-21', 'client_code' => 'C0282', 'client_name' => '国洋测试', 'action' => '接口名字' ], [ 'date' => '2019-06-21', 'client_code' => 'C0282', 'client_name' => '国洋测试', 'action' => '接口名字' ] ]; /** * [export 导出] * @param [type] $data [需要导出的数据] * @author [lingwei] [2019-06-21] * @return [type] [description] */ function export($data) { set_time_limit(0); ini_set('memory_limit','512M');//设置最大内存 //设置header头信息 header('Content-Type: text/csv'); header('Content-Disposition: attachment;filename="'. uniqid().'.csv"'); header('Cache-Control: max-age=0'); $headerObj=array( array('title'=>'时间', 'name'=>'date', 'type'=>'STRING'), array('title'=>'客户编码', 'name'=>'client_code', 'type'=>'STRING'), array('title'=>'客户名称', 'name'=>'client_name', 'type'=>'STRING'), array('title'=>'接口名字', 'name'=>'action', 'type'=>'STRING') ); $fp = fopen('php://output', 'a'); fwrite($fp,chr(0xEF).chr(0xBB).chr(0xBF)); foreach ($headerObj as $i => $v) { // CSV的Excel支持GBK编码,一定要转换,否则乱码 //$header[$i] = iconv('utf-8', 'gbk', $v['title']); $header[$i] = $v['title']; } fputcsv($fp, $header); // 计数器 $cnt = 0; // 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小 $limit = 10000; // 逐行取出数据,不浪费内存 $count = count($data); for($t=0;$t<$count;$t++) { $cnt ++; if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成问题 ob_flush(); flush(); $cnt = 0; } $i=0; foreach ($headerObj as $headKey => $headValue) { //$row[$i++]=iconv('utf-8', 'GB2312', $headValue['type']=='STRING'?'="'.$list[$t][$headValue['name']].'"':$list[$t][$headValue['name']]); $row[$i++] = $headValue['type'] == 'STRING' ? '="'.$data[$t][$headValue['name']].'"' : $data[$t][$headValue['name']]; } // foreach ($row as $i => $v) { // $row[$i] = iconv('utf-8', 'gbk', $v); // } fputcsv($fp, $row); unset($row); } fclose($fp); } #调用方式 export($data); ```