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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?