Sai A Sai A
Updated date Jul 20, 2023
In this blog, we will learn different methods to convert arrays to CSV files in PHP efficiently. This blog explores the usage of built-in functions like fputcsv(), manual formatting techniques, and the powerful League-Csv library.

Introduction:

Converting arrays to CSV (Comma-Separated Values) format is a common task in web development. CSV is a widely-used file format for storing and exchanging data between different systems. PHP, being a versatile scripting language, offers several efficient methods to accomplish this. In this blog post, we will explore multiple techniques for converting arrays to CSV files in PHP. 

Method 1: Utilizing the fputcsv() Function

The fputcsv() function in PHP is a built-in function specifically designed for writing arrays to CSV files. It automatically handles the formatting and escaping of values, making the conversion process straightforward. Let's take a look at an example program that demonstrates the usage of fputcsv() to convert an array to a CSV file:

<?php
$array = [
    ['John', 'Doe', '[email protected]'],
    ['Jane', 'Smith', '[email protected]'],
    ['Bob', 'Johnson', '[email protected]'],
];

$fp = fopen('output.csv', 'w');
foreach ($array as $row) {
    fputcsv($fp, $row);
}
fclose($fp);
?>

Output (output.csv):

John,Doe,[email protected]
Jane,Smith,[email protected]
Bob,Johnson,[email protected]

Method 2: Manual CSV Formatting

An alternative approach is to manually format the array and write it to a CSV file. This method provides greater control over the CSV generation process, albeit requiring more code. Here's an example program that demonstrates how to manually convert an array to a CSV file:

<?php
$array = [
    ['John', 'Doe', '[email protected]'],
    ['Jane', 'Smith', '[email protected]'],
    ['Bob', 'Johnson', '[email protected]'],
];

$fp = fopen('output.csv', 'w');
foreach ($array as $row) {
    fputcsv($fp, $row, ',', '"');
}
fclose($fp);
?>

Output (output.csv):

John,Doe,[email protected]
Jane,Smith,[email protected]
Bob,Johnson,[email protected]

Method 3: Using the League\Csv Library

The League\Csv library is a powerful and feature-rich package that simplifies working with CSV data in PHP. It provides a convenient API for reading and writing CSV files, making it an excellent choice for array to CSV conversions. To use the library, first install it via Composer:

composer require league/csv

Here's an example program that demonstrates how to utilize the League\Csv library to convert an array to a CSV file:

<?php
require 'vendor/autoload.php';

use League\Csv\Writer;

$array = [
    ['John', 'Doe', '[email protected]'],
    ['Jane', 'Smith', '[email protected]'],
    ['Bob', 'Johnson', '[email protected]'],
];

$csv = Writer::createFromPath('output.csv', 'w');
$csv->insertAll($array);
$csv->output();
?>

Output (output.csv):

John,Doe,[email protected]
Jane,Smith,[email protected]
Bob,Johnson,[email protected]

Conclusion:

Converting arrays to CSV in PHP can be achieved using various methods. In this blog post, we explored three approaches: utilizing the fputcsv() function, manual CSV formatting, and using the League\Csv library. The fputcsv() function is a simple and efficient solution, while manual formatting provides greater control. The League\Csv library offers advanced features for working with CSV files.

Comments (0)

There are no comments. Be the first to comment!!!