FacebookTwitter

Export Array to CSV – PHP

By on Aug 29, 2014 in PHP

Share On GoogleShare On FacebookShare On Twitter

Export to CSV – PHP

CSV (comma-separated values) is the most widely supported format for transferring tabular data. Exporting data in CSV format is very common in web applications. This simple example will help you get started with CSV and PHP. The main goal is to create a simple way to export data from your website or web application into a CSV that can be downloaded by the user.

The Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
function convert_to_csv($input_array, $output_file_name, $delimiter)
{
    /** open raw memory as file, no need for temp files, be careful not to run out of memory thought */
    $f = fopen('php://memory', 'w');
    /** loop through array  */
    foreach ($input_array as $line) {
        /** default php csv handler **/
        fputcsv($f, $line, $delimiter);
    }
    /** rewrind the "file" with the csv lines **/
    fseek($f, 0);
    /** modify header to be downloadable csv file **/
    header('Content-Type: application/csv');
    header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
    /** Send file to browser for download */
    fpassthru($f);
}
/** Array to convert to csv */
$array_to_csv = Array(
    Array(12566,
        'Enmanuel',
        'Corvo'
    ),
    Array(56544,
        'John',
        'Doe'
    ),
    Array(78550,
        'Mark',
        'Smith'
    )
);
convert_to_csv($array_to_csv, 'report.csv', ',');

The Explanation

[show_resp_ad]

The function convert_to_csv($input_array, $output_file_name, $delimiter) export to csv by taking in a PHP Array ($input_array) as its first parameters and converts it into a CSV. The other two parameters are used to specify the name of the CSV file, and a delimiter. The delimiter is used to separate the data, in this case it should be a comma, since we are trying to output “Comma Separate Values”.

The first thing this function does is take advantage of raw memory to temporally place the CSV file. The alternative to this is to store the file locally on the server and then remove it. But doing this in memory is much easier.

export to csv php

Next, it loops through the Array and puts the content into the variable we have in memory $temp_memory, splitting the content using the $delimiter. Then, it rewinds the content in memory, putting the file position indicator back at the beginning of the stream using default PHP fseek() function.

At this point or CSV is ready, all the is left to do now is download it. In order to download/Export the file, the function tells the browser how to interpret it content and also it tells the browser that we want to download not display the content of the file.

export to csv php

This simple function will save you time when you try to export to csv. You can simple add it to your project and call it just like the example. Hope you find this useful. Don’t forget to leave your thought as comments below.

You can find the code in GitHub Export to CSV – PHP

Enmanuel Corvo

Enmanuel Corvo

Software Engineer at LiveAnswer
Enmanuel Corvo is a Software Engineer experienced in Web Applications Development. Huge fan of WordPress and an active plug-in author who enjoys writing code and blog posts about anything related to technology.
Enmanuel Corvo

Latest posts by Enmanuel Corvo (see all)