欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PHP导出csv,防止内存泄漏 demo.php

程序员文章站 2022-07-13 13:16:43
...
<?php
/**
 * Created by PhpStorm.
 * User: kaiwu
 * Date: 2018/6/29
 * Time: 14:27
 */
//PHP大量数据导出方案(csv文件循环导出)
header('Content-disposition: attachment; filename=' . iconv('UTF-8', 'GBK', '结项导出信息') . date('Y_m_d_H_i_s') . '.csv');
$head = array(
    '城市',
    '姓名'
);
$head = u2g($head);
echo implode(',', $head)."\n";
$i = 0;
$count = 1000000;
do{
    $row = array();
    $row[0] = $i;
    $row[1] = $i+1;
    $lastArray = array();
    $lastArray[] = $row;
    foreach ($lastArray as $key => $val){
        echo implode(',', $val)."\n";
    }
    ob_flush();
    flush();
    $i=$i+2;
}while($i<=$count);


function u2g($in){
    if(is_array($in)) {
        foreach($in as $k=>$v) {
            $in[$k] = iconv('UTF-8','GBK//ignore',$v);
        }
    } else {
        $in = iconv('UTF-8','GBK//ignore',$in);
    }
    return $in;
}

上一篇: 进制转换

下一篇: 进制转换