2

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

Community
  • 1
  • 1
Param Kumar
  • 123
  • 3
  • 18

1 Answers1

1

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);
}
Sashant Pardeshi
  • 1,075
  • 7
  • 21