PHP Array to CSV
Paste a PHP array and get CSV. The array literal is parsed in the browser, so no PHP runs anywhere. A nested array inside a row is not flattened into columns: it lands in one cell as compact JSON text. Any value containing the delimiter, a quote or a line break is wrapped in quotes.
1 PHP Array to CSV example
PHP Array Input
2
3
4
5
[
['id' => 1, 'name' => 'John Doe', 'role' => 'Admin'],
['id' => 2, 'name' => 'Jane Smith', 'role' => 'User'],
['id' => 3, 'name' => 'Bob Johnson', 'role' => 'Editor']
] CSV Output
2
3
4
id,name,role
1,"John Doe",Admin
2,"Jane Smith",User
3,"Bob Johnson",Editor 2 Where it helps
Exporting without deploying
Export from the array you already have. Turn an array in a PHP file into a spreadsheet without writing and deploying an export route.
Handing data to someone else
CSV opens in Excel, Google Sheets and LibreOffice. A PHP array is only useful to someone running PHP.
Workflow
Exporting a PHP array list to CSV
- 01 The input is a PHP array of rows. Paste it into the left editor, or use Upload PHP or Load URL. Dummy Data shows the expected shape.
- 02 Choose the delimiter: comma, semicolon, tab or pipe. Semicolon is the safe choice for Excel outside the US and UK.
- 03 CSV appears on the right. Associative rows get a header line built from the union of their keys; plain lists get no header.
- 04 Values containing the chosen delimiter, a double quote or a line break are wrapped in double quotes automatically. A nested array inside a row goes into one cell as compact JSON text.
- 05 Click Copy CSV, or Download to save a .csv file.
In code
How to convert a PHP array to CSV with fputcsv
PHP
php<?php
$rows = [
['sku' => 'A-100', 'name' => 'Widget, large', 'price' => 9.99],
['sku' => 'B-200', 'name' => 'Gadget "Pro"', 'price' => 15],
];
$fh = fopen('products.csv', 'w');
fwrite($fh, "\xEF\xBB\xBF"); // UTF-8 BOM so Excel reads accents
fputcsv($fh, array_keys($rows[0]), ',', '"', '\\', "\r\n"); // header row
foreach ($rows as $row) {
fputcsv($fh, $row, ',', '"', '\\', "\r\n");
}
fclose($fh);
echo file_get_contents('products.csv'); Change the third argument to ';' for a semicolon file. Use fopen('php://output', 'w') with a Content-Disposition header to send it as a download.
Command line
bash# rows.php ends with: return [ ['sku' => 'A-100', ...], ... ];
php -r '$rows = include "rows.php"; $o = fopen("php://stdout", "w");
fputcsv($o, array_keys($rows[0]), ";", "\"", "\\");
foreach ($rows as $r) fputcsv($o, $r, ";", "\"", "\\");' > rows.csv A one-off export from a file that returns an array. Output goes to stdout; redirect it wherever you want.
Troubleshooting
Inconsistent row keys and nested value issues
- Array to string conversion on a row
- A cell holds a nested array. fputcsv() only writes scalars; flatten the row with implode(), json_encode() the cell, or drop the column.
- Excel puts every row in one cell
- Excel splits on the locale's list separator, a semicolon in most of Europe. Write the file with ';' or tell users to import via Data > From Text/CSV.
- Umlauts and accents are garbled in Excel
- Excel assumes a legacy code page unless the file starts with a UTF-8 BOM. Write "\xEF\xBB\xBF" before the first fputcsv() call, as in the snippet.
- Deprecated: fputcsv(): the $escape parameter must be provided
- PHP 8.4 requires an explicit escape argument. Pass '\\' to keep the old behavior or '' to follow RFC 4180 exactly.
Format notes
Preparing associative rows for Excel
fputcsv() ends lines with LF. RFC 4180 says CRLF, and CRLF is what Excel writes. Since PHP 8.1 the sixth argument (eol) lets you pass "\r\n". LF breaks nothing in most tools; the reason to match CRLF is to avoid a full-file diff every time the CSV round-trips through Excel.
Guard against CSV injection when the data came from users. fputcsv() quotes only when it has to, so a value like =SUM(A1) goes out bare and Excel runs it as a formula when the file opens. Prefix any cell that starts with =, +, - or @ with an apostrophe.
Docs
PHP fputcsv and RFC 4180 docs
Questions
PHP to CSV questions
What array shape works best?
An array of associative arrays with the same keys. Each inner array becomes a row, and the keys become the header row.
Can I choose the delimiter?
Yes. Comma, semicolon, tab and pipe are available. Use semicolon for spreadsheets set to a European locale. Any value that contains the chosen delimiter, a double quote or a line break is wrapped in quotes, so it stays in one cell.
How are nested arrays handled?
The converter expects two levels: an outer list of rows, each an associative array. A nested array inside a row is not flattened into columns. It is written into one cell as compact JSON text, such as ["a","b"]. For deeper data, use the PHP Array to JSON tool, which keeps any depth.
Are both array() and [] syntaxes supported?
Yes. The parser accepts either form, including mixed usage inside the same input.
Can I download the CSV?
Yes. Download saves a .csv file that opens in Excel, Google Sheets or LibreOffice.