Skip to content
ZeroServer.tools

JSON to PHP Array

Convert JSON into a PHP array using short-array syntax — in your browser.

or drop a file here
PHP array
$data = [
    'name' => 'Ada',
    'active' => true,
    'roles' => [
        'admin',
        'editor',
    ],
    'profile' => [
        'age' => 36,
        'city' => 'London',
    ],
];

JSON to PHP arrays

This converts a JSON document into a PHP value using modern short-array syntax ([ ]): JSON objects become associative arrays ('key' => value) and JSON arrays become list arrays. It's handy for seeding config or fixtures from an API response. Parsing happens entirely in your browser.

Related: the JSON formatter, the JSON to TypeScript generator, and the JSON to YAML converter.

Built and maintained by Meet Shah · Last updated

What this tool is used for

  • Pasting an API response into a PHP script as an array literal for a quick test.
  • Producing a fixture array for a test without loading a file.
  • Converting a config sample into a PHP default value.
  • Turning a payload into short-array syntax for a code review.
  • Checking how JSON null and booleans map into PHP.

Frequently Asked Questions

Why not just call json_decode?
Use it for data at runtime — always. This produces a literal to paste into source: a config file, a test fixture, a database seeder. The difference is when parsing happens, and a literal in the file means no parse at all and no malformed string failing at boot.
Why the short array syntax?
`[...]` has been valid since PHP 5.4 and is what every current style guide uses; `array(...)` is the older spelling of exactly the same thing. There is no behavioural difference — the short form is simply what a modern codebase reads like.
How do JSON objects and arrays both become arrays?
Because PHP has one array type doing both jobs — an ordered map. A JSON object becomes a string-keyed array with `=>` arrows and a JSON array becomes a list with implicit integer keys. It is also why `json_decode` needs a second argument to say which you wanted back.
How are strings quoted?
Single quotes, with backslashes doubled and apostrophes escaped. Single quotes are deliberate: PHP does not interpolate variables inside them, so a value containing `$name` stays literal instead of being substituted with whatever happens to be in scope.
What about a trailing comma on the last element?
It is included, and it has been valid PHP in arrays for a very long time. It keeps a diff to one line when an element is appended, which is why most style guides now require it rather than merely permitting it.

Common errors and gotchas

  • Forgetting that PHP arrays conflate lists and maps, so a JSON array and object can look alike in the output.
  • Overlooking that JSON `null` becomes PHP `null`, which behaves differently from an empty string in comparisons.
  • Losing large integer precision, which was already lost if the JSON came from JavaScript.
  • Producing a literal so large it belongs in a file rather than in source.
  • Assuming numeric string keys stay strings, when PHP casts them to integers.

Related Converters tools

Private & free — this tool runs entirely in your browser.

IndieKitShip your Next.js startup in days.affiliate