JSON to PHP Array

Paste a JSON document and get a PHP array literal formatted for a .php source file, short array syntax ([...]), proper indentation, strings quoted correctly, nested objects becoming associative arrays and arrays staying indexed. Useful for embedding API fixtures in tests or seeding config files.

How to convert JSON to PHP

  1. 1

    Paste the JSON

    Any valid JSON: objects, arrays, deeply nested.

  2. 2

    Pick syntax

    Short `[...]` (PHP 5.4+) or long `array(...)` (rarely needed now).

  3. 3

    Pick string quotes

    Single-quoted (no interpolation, escape apostrophes) or double-quoted (allows `$` interpolation: usually not what you want for data).

  4. 4

    Copy the array

    Paste into your `.php` file, assign to a `return`, or wrap in `config([...])`.

Example

Input:

{ "app": { "name": "Hyperion", "debug": true, "hosts": ["h1.example.com", "h2.example.com"] } }

Output (short syntax, single quotes):

[
    'app' => [
        'name' => 'Hyperion',
        'debug' => true,
        'hosts' => [
            'h1.example.com',
            'h2.example.com',
        ],
    ],
]

Type mapping

JSON PHP
string string (single-quoted)
integer int
number (decimal) float
boolean true / false
null null
array (JSON) indexed array
object (JSON) associative array

When to use associative vs indexed

PHP has one array type with both behaviours. JSON clearly distinguishes object and array, so the mapping is:

  • {} -> associative (['key' => value]).
  • [] -> indexed ([value, value]).

The generator always preserves the distinction; mixing the two in PHP is legal but confusing.

PHP-specific gotchas

  • Numeric-string keys. PHP casts "42" to the integer key 42 in an array. If your JSON uses numeric strings as keys and you need them as strings, the array_combine round-trip will not preserve them.
  • Large integers. JSON can hold integers larger than PHP_INT_MAX on 32-bit builds. The generator warns when that might happen.
  • UTF-8. PHP strings are byte sequences; the generator emits UTF-8 literally. Ensure your .php file is saved as UTF-8 without BOM.

Use cases

  • Config files: Laravel and Symfony both use PHP-array config. Dropping a JSON config vendor-style works well.
  • Test fixtures: PHPUnit fixtures are cleaner as arrays than as JSON files loaded at runtime.
  • Seeds: database seeders in Laravel often start as a JSON dump and become PHP arrays.

Common mistakes

  • Round-tripping PHP arrays back to JSON. json_encode treats an associative array as an object and an indexed array as an array. If you mix them (['key' => 'val', 0 => 'x']), you get an object on the way out. Keep JSON-shaped arrays JSON-shaped.
  • Double quotes with apostrophes. Single-quoted PHP strings need escaped \'; double-quoted strings need escaped \\" and variables interpolated accidentally if you are not careful. Stick to single quotes for data.
  • Forgetting trailing commas. PHP allows them since PHP 7.3 and they make diffs cleaner. The generator adds them by default.

Frequently Asked Questions

Short syntax ([...]) has been available since PHP 5.4 and is the idiomatic choice for all modern code. Long syntax (array(...)) is needed only for code that must run on PHP 5.3 or earlier, essentially legacy systems.

Yes. Laravel config files are plain PHP arrays returned from a file. Paste the generator output after return and save it in config/.

Single-quoted PHP strings do not interpolate variables or escape sequences (except \' and \\). For data literals, this is faster and safer. Double quotes only matter when you want \n, \t, or $variable inside the string.

No, it emits an array literal. Wrap it yourself with return, const, or a class property as needed. Keeping the output small keeps it composable.

Related Tools

Tool available in other languages