【问题标题】:create and download the csv from array with my own header in php在 php 中使用我自己的标头从数组中创建和下载 csv
【发布时间】:2016-03-18 04:01:24
【问题描述】:

我有一个非常简单的数组,我想将它导出到一个 CSV 文件中,我在 How to create and download a csv file from php script? 的帮助下完成了这项工作

但我希望文件顶部有我自己的标题,它也是一个数组,我自己创建的,看起来像这样-

           $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'
                    );

我原来的导出数组是-

 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 工作正常我只想在原始数组的顶部添加这个标题,这样当我导出它时,我会在我的 csv 文件中获得数组的第一行作为标题。任何机构都可以有一些技巧来实现这一结果

【问题讨论】:

  • 能否请您发布您的代码,它可能会帮助我们为您提供解决方案。

标签: php arrays csv export


【解决方案1】:

你可以试试下面的代码。

假设您使用的是您提到的代码 - 如何从 php 脚本创建和下载 csv 文件? ,请将您的 $header 数组作为第四个参数传递给函数 array_to_csv_download。

然后在写入导出数组之前,将头数组写入csv文件。

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多