CSV to PHP Array Converter
Paste CSV and get ready-to-paste PHP array code — short [] syntax, classic array(), or var_export style — with header-row associative keys, delimiter auto-detect, and optional number/boolean type casting. Copy the output or download it as a .php file. Free, unlimited, 100% client-side: your data never leaves this browser.
Convert CSV to PHP array
var_export style always uses PHP-native 2-space indent, like var_export() itself.
Live preview (first 20 rows × 10 columns)
PHP array code appears here…
Download prepends <?php so the file is a valid PHP script; copy gives you just the code for pasting into existing files.
How to convert CSV to a PHP array
- Paste or drop your CSV into the converter above (or load the sample to see edge cases in action).
- Set the delimiter — leave Auto-detect on, or pick comma, semicolon, tab, pipe, or a custom character.
- Choose row mode — keep “First row is header” ticked for associative arrays keyed by column name, or untick it for plain indexed rows.
- Pick the PHP style — short
[](PHP 5.4+), classicarray(), orvar_export— plus value types (all strings vs auto-cast) and the wrapper (assignment,return, or bare literal). - Check the preview, then copy the highlighted PHP or download it as a
.phpfile.
Unlike server-side converters, this page needs no signup and no upload — the RFC 4180 parser and the PHP code generator are plain JavaScript running entirely in your browser, so it also works offline once loaded and handles files other converters choke on (multiline quoted fields, escaped quotes, mixed delimiters).
Worked examples
Every output below was generated by the same engine that powers the converter above — including the tricky bits: quoted commas, escaped quotes, multiline fields, and PHP string escaping.
Example 1: Basic contact list Header row becomes array keys, short [] syntax, auto-cast types.
Input CSV short [], assoc, auto-cast
name,age,city Alice,30,Berlin Bob,25,Paris
Generated PHP
$contacts = [
0 => [
'name' => 'Alice',
'age' => 30,
'city' => 'Berlin',
],
1 => [
'name' => 'Bob',
'age' => 25,
'city' => 'Paris',
],
];
Example 2: Commas inside quoted fields A quoted field may contain the delimiter — RFC 4180.
Input CSV short [], assoc, strings
name,notes "Doe, John","likes apples, oranges"
Generated PHP
$data = [
0 => [
'name' => 'Doe, John',
'notes' => 'likes apples, oranges',
],
];
Example 3: Escaped double quotes Doubled quotes inside a quoted field become one quote.
Input CSV short [], assoc, strings
id,quote 1,"She said ""hello"""
Generated PHP
$data = [
0 => [
'id' => '1',
'quote' => 'She said "hello"',
],
];
Example 4: Multiline address field Line breaks inside quotes are kept as literal newlines in the PHP string.
Input CSV short [], assoc, strings
name,address "Acme Co","123 Main St Springfield"
Generated PHP
$data = [
0 => [
'name' => 'Acme Co',
'address' => '123 Main St
Springfield',
],
];
Example 5: Apostrophes and backslashes Single quotes and backslashes are escaped exactly like PHP does.
Input CSV short [], assoc, strings
name,path O'Brien,C:\Users\obrien
Generated PHP
$data = [
0 => [
'name' => 'O\'Brien',
'path' => 'C:\\Users\\obrien',
],
];
Example 6: var_export style output Byte-identical in spirit to PHP's own var_export() output.
Input CSV var_export style, assoc, auto-cast
sku,price,in_stock A-1,9.99,true
Generated PHP
$products = array (
0 =>
array (
'sku' => 'A-1',
'price' => 9.99,
'in_stock' => true,
),
);
Example 7: Semicolon CSV, indexed rows Auto-detect picks up the semicolon; rows stay indexed without a header.
Input CSV short [], indexed, auto-cast
10;20;30 40;50;60
Generated PHP
$rows = [
0 => [
0 => 10,
1 => 20,
2 => 30,
],
1 => [
0 => 40,
1 => 50,
2 => 60,
],
];
Frequently Asked Questions
How do I convert CSV to a PHP array online?
Should I use var_export style or the short [] syntax?
How are data types handled — do numbers stay strings?
How are single quotes and backslashes escaped in the output?
What happens to empty cells — empty string or null?
How do I generate a Laravel seeder from a CSV?
Does it handle commas inside quoted fields or multiline values?
Is my CSV data uploaded to a server?
Free tool by NRTechStudio