把数据库里面的数据导出来csv

function export_csv($filename, $data, $head = '')
{
    $string = $head;
    foreach ($data as $key => $value) {
        foreach ($value as $k => $val) {
            $value[$k] = iconv('utf-8', 'utf-8', $value[$k]);
        }

        $string .= implode(",", $value) . PHP_EOL; //用英文逗号分开
    }

    header("Content-type:text/csv");
    header("Content-Disposition:attachment;filename=" . $filename);
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    echo $string;
}
posted @ 2018-03-20 15:23  Thomas_188  阅读(185)  评论(0编辑  收藏  举报