Write PHP Array to CSV File

PHP can write many different types of arrays to CSV files. To achieve this task we can use some of the native PHP functions that are at our disposal. These functions include, fopen(), fputcsv() and fclose(). In this tutorial, we will learn how to loop over an array and write the data to a CSV file.

There are plenty of libraries out there that can achieve this task but most are full of bloat and take a lot longer than the native functions available.

Associative Array To CSV Method

In the associative method, we need will use the associative identifiers as the files column headers and then write in the values that belong to them

PHP Code

$f = fopen('mycsv.csv', 'a'); // Configure fopen to create, open, and write data.

fputcsv($f, array_keys($data[0])); // Add the keys as the column headers

// Loop over the array and passing in the values only.
foreach ($data as $row)
{
    fputcsv($f, $row);
}
// Close and initiate download.
fclose($f);

Breakdown of the code –

  • First, we use the fopen() function to open the file and write to it. This uses two parameters
    • ‘mycsv.csv’ – Essentially the identifying name and full path – This will create the file in the same directory as the script.
    • ‘a’  to instruct fopen to create the file if it doesn’t exist and instruct that we are wanting to write only.
  • Then we need to add the initial array headers or column headers. We do this by using fputcsv() outside the loop, just passing in the keys.
  • From here we start to loop over the entire array calling to fputcsv() again but only passing in the ‘rows’ or values.
  • Finally, we instruct PHP to close the file, and after this is executed, the comma-seperated value typed-file will be downloaded to our local machine.

Indexed Array To CSV Method

This method takes less code than the above method due to no keys being present so therefore we don’t need to worry about them. In this use case, we only loop in the actual values.

PHP Code

$f = fopen('mycsv.csv', 'a'); // Configure fOpen to create, open and write only.

// Loop over the array and passing in the values only.
foreach ($data as $row)
{
    fputcsv($f, $row);
}
// Close and initiate download.
fclose($f);

Breakdown of the code –

  • First, we use the fopen() function to open the file and write to it. This uses two parameters
    • ‘mycsv.csv’ – Essentially the identifying name and full path and full path – This will create in the same directory as the script.
    • ‘a’  to instruct fopen to create if it doesn’t exist and instruct that we are wanting to write only.
  • From here we start to loop over the entire indexed array calling to fputcsv() passing in each value.
  • Finally, we instruct PHP to close the file, and after this is executed it will be downloaded to our local machine.

Function References

Summary

In this tutorial we explored the native capabilities of PHP, using file-writing functions that allow us to quickly convert array-data into specified file-types. PHP is more than capable of doing these jobs and doing so extremely quickly.

Posted in PHP

Leave a Reply

Your email address will not be published.