导出csv
<?php
# TODO 增加内存限制值
ini_set('memory_limit', '2048m');
/**
* 导入CSV
* @param array $aData
* @param array $aTitle
* @param string $sFileName
* @return boolean
*/
/*
#TODO case:
$aData = array(
array('id'=>1,'name'=>'名称1'),
array('id'=>2,'name'=>'名称2'),
array('id'=>3,'name'=>'名称3'),
);
$aTitle = array(
array('id','标记'),
array('name','名称'),
);
exportCSV($aData, $aTitle);
*/
function exportCSV($aData = [], $aTitle = [], $sFileName=false)
{
if (!is_array($aData) || !is_array($aTitle))
return false;
if (empty($aData) || empty($aTitle))
return false;
$sFileName = $sFileName ? mb_convert_encoding($sFileName, "GB2312", "UTF-8, GB2312") . ".csv": date("_YmdHis") . ".csv";
header('Content-Type: text/csv; CHARSET=gb2312');
header('Content-Disposition: attachment; filename=' . $sFileName);
$output = fopen('php://output', 'w');
for ($i=0;$i<count($aData);$i++) {
for($j=0;$j<count($aTitle);$j++){
$aList[$i][$j] = mb_convert_encoding($aData[$i][$aTitle[$j][0]], "GB2312", "UTF-8, GB2312");
}
}
for ($i=0;$i<count($aTitle);$i++) {
$aTitle[$i] = mb_convert_encoding($aTitle[$i][1], "GB2312", "UTF-8, GB2312");
}
fputcsv($output, $aTitle);
foreach ($aList as $key) {
fputcsv($output, $key);
}
return true;
}