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 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线