dtuzjzs3853 2015-12-12 06:42
浏览 21
已采纳

在php中使用我自己的头创建并从数组中下载csv

I have very simple array which I want to export into a CSV file, which I alomost done with the help of this How to create and download a csv file from php script?

But I want my own header at the top of the file which is also a array, created myself and looks like this-

           $header = array(
                    'order_id'                => 'order_id',
                    'firstname'               => 'firstname',
                    'lastname'                => 'lastname',
                    'email'                   => 'email',
                    'telephone'               => 'telephone',
                    'fax'                     => 'fax',
                    'product_id'              => 'product_id',
                    'name'                    => 'name',
                    'model'                   => 'model',
                    'quantity'                => 'quantity',
                    'price'                   => 'price',
                    'currency_value'          => 'total',
                    'order_status'            => 'order_status'
                    );

my original export array is -

 Array
 (
[0] => Array
    (
        [order_id] => 195
        [firstname] => satyendra
        [lastname] => singh
        [email] => satyendra.singh43@gmail.com
        [telephone] => 9818077908
        [fax] => 
        [product_id] => 7086
        [name] => Bachini  Casual Shoes  1511-Navy Blue
        [model] => 1511-Navy Blue
        [quantity] => 1
        [price] => 499.00
        [currency_value] => 499.00
        [order_status] => 
    )

   [1] => Array
    (
        [order_id] => 196
        [firstname] => satyendra
        [lastname] => singh
        [email] => satyendra.singh43@gmail.com
        [telephone] => 9818077908
        [fax] => 
        [product_id] => 7086
        [name] => Bachini  Casual Shoes  1511-Navy Blue
        [model] => 1511-Navy Blue
        [quantity] => 1
        [price] => 499.00
        [currency_value] => 499.00
        [order_status] => 
    )

 )

export is working fine I just want to add this header at the top of my original array so that when I export this I get first row of array as header in my csv file. can any body have some trick to achieve that result

  • 写回答

1条回答 默认 最新

  • duanhanzi8328 2015-12-12 07:29
    关注

    You may please try below code.

    Assuming you are using code you mentioned - How to create and download a csv file from php script? , please pass your $header array as fourth parameter to function array_to_csv_download.

    Then just before export array is written, header array is written to csv file.

    function array_to_csv_download($array, $filename = "export.csv", $delimiter=";",$header) {
        // open raw memory as file so no temp files needed, you might run out of memory though
        $f = fopen('php://memory', 'w');
        fputcsv($f, $header, $delimiter); // write $header array first to top of csv file
        // loop over the input array
        foreach ($array as $line) { 
            // generate csv lines from the inner arrays
            fputcsv($f, $line, $delimiter); 
        }
        // reset the file pointer to the start of the file
        fseek($f, 0);
        // tell the browser it's going to be a csv file
        header('Content-Type: application/csv');
        // tell the browser we want to save it instead of displaying it
        header('Content-Disposition: attachment; filename="'.$filename.'";');
        // make php send the generated csv lines to the browser
        fpassthru($f);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀