JSON to PHP Array

Paste any JSON and get clean, ready-to-paste PHP array code — nested objects, arrays, and all.

About JSON to PHP Array Converter

This tool converts any valid JSON into a ready-to-paste PHP array. JSON objects become PHP associative arrays (string keys), JSON arrays become PHP indexed arrays, and nested structures are preserved with proper indentation. Booleans, numbers, and null are mapped to their PHP equivalents.

Everything runs 100% in your browser — your JSON data never leaves your device, making it safe to use with confidential API responses, internal configs, or sensitive payloads.

How to Use

  1. Paste your JSON into the left-hand editor, or click Load Sample to try it out.
  2. Pick your options: variable name, short [] vs long array() syntax, quote style, indent width, trailing comma, and whether to include a <?php tag.
  3. View the output on the right — it updates automatically as you type or tweak options.
  4. Copy the result with the Copy button and paste it straight into your PHP file.

Type Mapping

JSON → PHP

  • object → associative array
  • array → indexed array
  • string → quoted string
  • number → int or float

Literals

  • truetrue
  • falsefalse
  • nullnull
  • Empty objects/arrays → []

Example

JSON Input

{
  "user": {
    "id": 1,
    "name": "Alice"
  },
  "tags": ["admin", "user"]
}

PHP Array Output

$data = [
    'user' => [
        'id' => 1,
        'name' => 'Alice',
    ],
    'tags' => [
        'admin',
        'user',
    ],
];

Common Use Cases

  • API integration: Paste a JSON response into a PHP project as a fixture or seed.
  • Laravel / Symfony configs: Convert JSON config snippets into config/*.php return arrays.
  • WordPress development: Turn JSON data into PHP arrays for themes, plugins, or block registration.
  • Testing & mocks: Embed JSON payloads in PHPUnit tests as fully-typed PHP arrays.
  • Migrating codebases: Quickly port JSON data files to PHP equivalents.

Frequently Asked Questions

What is the difference between short [] and long array() syntax?

Both declare a PHP array. Short syntax [] was introduced in PHP 5.4 and is now the conventional choice — it's cleaner and matches most modern style guides (PSR-12, Laravel, Symfony). Long syntax array() is still valid and required if you're maintaining legacy code that targets PHP < 5.4.

Should I use single or double quotes for strings?

In PHP, single-quoted strings are literal and slightly faster because PHP doesn't parse them for variable interpolation or most escape sequences. Double-quoted strings allow $variable interpolation and escape sequences like \n. For plain data arrays, single quotes are the safer default.

Does this tool handle deeply nested JSON?

Yes. The converter walks the entire JSON tree recursively, producing properly indented nested arrays at any depth. Each level is indented by your chosen indent width (2 spaces, 4 spaces, or a tab).

How are JSON arrays converted to PHP?

JSON arrays become PHP indexed arrays without explicit numeric keys — PHP auto-indexes them starting from 0. JSON objects become associative arrays with string keys in the form 'key' => value.

Why include a trailing comma?

Trailing commas are valid in PHP arrays and recommended by most style guides. They make diffs cleaner (adding a new line is a one-line diff instead of two) and reduce syntax errors when reordering items. PHP 7.3+ also supports trailing commas in function calls.

Is my JSON data sent to a server?

No. The entire conversion runs in your browser using JavaScript. Your JSON never leaves your device, so it's safe to use with confidential API responses, internal configs, or any sensitive payload.

How do I use the output in a PHP file?

Enable the <?php tag option for a standalone file, or leave it off and paste the $data = [...] snippet into an existing PHP file. For Laravel/Symfony config files, change the assignment to return [...]; after pasting.

What PHP version is the output compatible with?

Short array syntax [] requires PHP 5.4+. Long syntax array() works on every PHP version. The => key/value operator, all scalar types, and associative arrays work in all modern PHP versions.

How are special characters in strings handled?

Backslashes and matching quote characters are escaped automatically so the generated string is a valid PHP literal. If you pick double quotes, dollar signs and common escape sequences (\n, \r, \t) are escaped too, so no accidental interpolation occurs.