foreach($machineData as $key => $val){
if ($val['MeasureUpReferKey'] != ''){
$partnerArr[$val['MeasureUpReferKey']] = true;
}
$partnerArr[$val['BelongToReferKey']] = true;
if ($val['MerchantID'] != ''){
$merchantsArr[$val['MerchantID']] = true;
}
$organizationArr[$val['BelongToReferKey']] = true;
}
$parintKEY = '';
$merintKEY = '';
$orgintKEY = '';
foreach ($merchantsArr as $key =>$value){
$merintKEY.="'{$key}',";
}
foreach ($partnerArr as $key =>$value){
$parintKEY.="'{$key}',";
}
foreach ($organizationArr as $key =>$value){
$orgintKEY.="'{$key}',";
}
循环处理 取数据 方法
//金额显示
number_format($v['PayAmount']/100,2)
//时间转时间戳
strtotime
//手机号中间四位 ****
substr_replace($v['PartnerMobile'], '****', 3, 4)
//
?导出CSV格式文件
?
//标题头 title内容
$headerList = array();
//文本内容
$machineData = array();
foreach ($res['title'] as $k => $v){
$headerList[] = $v;
}
foreach ($res['allList'] as $k => $v){
foreach ($v as $key => $val){
$machineData[$k][] = $val;
}
}
$result = array();
// 打开文件资源,不存在则创建
$time = time();
$fileName = date('Ym',time()).$time;
$des_file = './DownLoad/'.$username.$fileName.'_export.csv';
$fp = fopen($des_file,'a');
// 处理头部标题
$header = implode(',', $headerList) . PHP_EOL;
// 处理内容
$content = '';
foreach ($machineData as $k => $v) {
$content .= implode(',', $v) . PHP_EOL;
}
// 拼接
$csv = $header.$content;
// 写入并关闭资源
fwrite($fp, $csv);
fclose($fp);
//把文件输出到下载
// $file = fopen($des_file,"r"); // 打开文件
// $size=filesize($des_file);
// Header("Content-type: application/octet-stream");
// Header("Accept-Ranges: bytes");
// Header("Accept-Length: ".$size);
// Header("Content-Disposition: attachment; filename=download.csv");
// echo fread($file,$size);
// fclose($file);
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on")
{
$pageURL .= "s";
}
$pageURL .= "://";
$pageURL .= $_SERVER["SERVER_NAME"];
$result['status'] = 200;
$result['msg'] = '成功';
$result['url'] = $pageURL.'/DownLoad/'.$username.$fileName.'_export.csv';
return $result;
|