PHP How to Convert Array to CSV File or String
Use
fputcsv() to write an array as a CSV line to a file, or use implode(",", $array) to convert an array to a CSV string in PHP.Examples
Input["apple", "banana", "cherry"]
Outputapple,banana,cherry
Input[["name", "age"], ["John", 30], ["Jane", 25]]
Outputname,age
John,30
Jane,25
Input[]
Output
How to Think About It
To convert an array to CSV in PHP, decide if you want a CSV string or a CSV file. For a string, join array elements with commas using
implode. For a file, open it and write each array row as a CSV line using fputcsv which handles commas and quotes properly.Algorithm
1
Get the input array.2
If writing to a file, open the file in write mode.3
For each row in the array, write it as a CSV line using <code>fputcsv</code>.4
If creating a string, join array elements with commas using <code>implode</code>.5
Close the file if opened.6
Return or output the CSV string or file.Code
php
<?php $array = ["apple", "banana", "cherry"]; // Convert array to CSV string $csvString = implode(",", $array); echo $csvString; // Convert 2D array to CSV file $data = [["name", "age"], ["John", 30], ["Jane", 25]]; $file = fopen('output.csv', 'w'); foreach ($data as $row) { fputcsv($file, $row); } fclose($file); ?>
Output
apple,banana,cherry
Dry Run
Let's trace converting ["apple", "banana", "cherry"] to CSV string using implode.
1
Input array
["apple", "banana", "cherry"]
2
Join elements
"apple" + "," + "banana" + "," + "cherry"
3
Output CSV string
"apple,banana,cherry"
| Step | Action | Result |
|---|---|---|
| 1 | Input array | ["apple", "banana", "cherry"] |
| 2 | Join with commas | "apple,banana,cherry" |
| 3 | Output string | apple,banana,cherry |
Why This Works
Step 1: Using implode for CSV string
implode joins array elements with commas to create a CSV line as a string.
Step 2: Using fputcsv for CSV file
fputcsv writes an array as a properly formatted CSV line to a file, handling commas and quotes automatically.
Step 3: File handling
Open the file with fopen, write lines with fputcsv, then close it with fclose to save changes.
Alternative Approaches
Manual string building
php
<?php $array = ["apple", "banana", "cherry"]; $csv = ''; foreach ($array as $item) { $csv .= '"' . str_replace('"', '""', $item) . '",'; } $csv = rtrim($csv, ','); echo $csv; ?>
Manually adds quotes and escapes quotes inside values but is more error-prone than fputcsv.
Using output buffering
php
<?php $data = [["name", "age"], ["John", 30], ["Jane", 25]]; ob_start(); $file = fopen('php://output', 'w'); foreach ($data as $row) { fputcsv($file, $row); } fclose($file); $csvString = ob_get_clean(); echo $csvString; ?>
Captures CSV output as a string without writing to a physical file.
Complexity: O(n) time, O(n) space
Time Complexity
The code loops through each element or row once, so time grows linearly with array size.
Space Complexity
Extra space depends on the output string or file buffer, proportional to input size.
Which Approach is Fastest?
implode is fastest for simple 1D arrays to string; fputcsv is best for files and complex data.
| Approach | Time | Space | Best For |
|---|---|---|---|
| implode | O(n) | O(n) | Simple 1D array to CSV string |
| fputcsv | O(n) | O(n) | Writing arrays to CSV files with proper formatting |
| Manual string building | O(n) | O(n) | Custom formatting but error-prone |
Use
fputcsv for reliable CSV formatting when writing files.Trying to join arrays with commas without handling quotes or nested arrays causes invalid CSV.