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.
[
['id' => 1, 'name' => 'John Doe', 'role' => 'Admin'],
['id' => 2, 'name' => 'Jane Smith', 'role' => 'User'],
['id' => 3, 'name' => 'Bob Johnson', 'role' => 'Editor']
] id,name,role
1,"John Doe",Admin
2,"Jane Smith",User
3,"Bob Johnson",Editor Export from the array you already have. Turn an array in a PHP file into a spreadsheet without writing and deploying an export route.
CSV opens in Excel, Google Sheets and LibreOffice. A PHP array is only useful to someone running 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.
# 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.
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.
This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.
An array of associative arrays with the same keys. Each inner array becomes a row, and the keys become the header row.
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.
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.
Yes. The parser accepts either form, including mixed usage inside the same input.
Yes. Download saves a .csv file that opens in Excel, Google Sheets or LibreOffice.
CSV to PHP array converts in the other direction, PHP array to JSON targets an API instead, and PHP array to Table shows the data without converting it.