Skip to main content

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

Drop a .csv file here or

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

  1. Paste or drop your CSV into the converter above (or load the sample to see edge cases in action).
  2. Set the delimiter — leave Auto-detect on, or pick comma, semicolon, tab, pipe, or a custom character.
  3. Choose row mode — keep “First row is header” ticked for associative arrays keyed by column name, or untick it for plain indexed rows.
  4. Pick the PHP style — short [] (PHP 5.4+), classic array(), or var_export — plus value types (all strings vs auto-cast) and the wrapper (assignment, return, or bare literal).
  5. Check the preview, then copy the highlighted PHP or download it as a .php file.

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?
Paste your CSV into the input box or drop a .csv file onto the drop zone. Leave the delimiter on Auto-detect, keep "First row is header" ticked if your top row holds column names, then choose an output style — short [], array(), or var_export — and copy the generated PHP or download it as a .php file. Everything runs in your browser; nothing is uploaded.
Should I use var_export style or the short [] syntax?
Use var_export style when you want output that mirrors PHP's own var_export() — array ( ... ) blocks with uppercase NULL — for example when diffing against var_export() output or feeding code generators that expect it. Use the short [] syntax (PHP 5.4+) for everyday code: it is what Laravel seeders, config files, and modern codebases use, and it reads far better. The array() option exists only for legacy PHP 5.3 code.
How are data types handled — do numbers stay strings?
You choose. "Keep all as strings" single-quotes every value — the safest default, and exactly what PHP's fgetcsv() hands you. "Auto-cast" converts integers to int, decimals to float, true/false (any case) to bool, and empty cells to null or '' depending on your empty-cell setting. Guard rails keep you honest: values with leading zeros like 007 or ZIP code 02139 stay strings, integers beyond the safe 64-bit range stay strings, and floats keep full double precision.
How are single quotes and backslashes escaped in the output?
Exactly the way PHP single-quoted strings work: a backslash becomes \\ and a single quote becomes \'. So O'Brien renders as 'O\'Brien' and C:\path as 'C:\\path'. Double quotes need no escaping inside single-quoted strings, and line breaks from multiline CSV fields are kept as literal newlines — the same thing var_export() does.
What happens to empty cells — empty string or null?
Your choice. Tick "Empty cells → null" to emit null (uppercase NULL in var_export style, exactly like PHP itself), or leave it off for ''. In auto-cast mode the literal text null or NULL in a cell also becomes null.
How do I generate a Laravel seeder from a CSV?
Keep "First row is header" on, pick the short [] syntax with auto-cast types, set the wrapper to "Bare array", then paste the result inside DB::table('your_table')->insert([...]); — the generated code drops straight in. For a config or fixture file, use the "return [...]" wrapper and save the download as config/data.php.
Does it handle commas inside quoted fields or multiline values?
Yes. The parser follows RFC 4180: a double-quoted field may contain commas, line breaks, and doubled quotes, so "Doe, John" stays one field and "She said ""hi""" becomes She said "hi". Delimiter auto-detection covers comma, semicolon, tab, and pipe; rows with the wrong field count and unterminated quotes are reported with their source line numbers.
Is my CSV data uploaded to a server?
No. This converter is 100% client-side JavaScript: parsing, PHP code generation, the live preview, and the .php download all run in your browser with zero network requests. Closing the tab erases your data; nothing is sent or stored anywhere.

Free tool by NRTechStudio