zoukankan      html  css  js  c++  java
  • php导出数组到csv格式demo

    php的二维数组导出到csv需要处理文字编码,代码如下

    复制代码
    <?php
    $data=array(
        array("username"=>"test1","password"=>"123"),
        array("username"=>"test2","password"=>"456"),
        array("username"=>"test3","password"=>"789"),
    );
    export_csv($data);
    function export_csv($data)
    {
        $string="";
        foreach ($data as $key => $value) 
        {
            foreach ($value as $k => $val)
            {
                $value[$k]=iconv('utf-8','gb2312',$value[$k]);
            }
    
            $string .= implode(",",$value)."
    "; //用英文逗号分开 
        }
        $filename = date('Ymd').'.csv'; //设置文件名
        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; 
    }
    ?>
    复制代码

     

  • 相关阅读:
    linux安装jdk1.8
    Python中import
    Python时间
    Python学习Json
    Hive命令学习
    Hadoop系统中的一些概念
    Hadoop系统命令
    ssh无密码登录设置
    Python学习
    Linux Socket IPC
  • 原文地址:https://www.cnblogs.com/zhangjianqiang/p/7847018.html
Copyright © 2011-2022 走看看