JSON to PHP Array Converter

Convert JSON into a PHP array literal you can paste into a config file or a test fixture.

JSON
PHP
Runs locally in your browser

About the JSON to PHP Array Converter

At runtime you would call json_decode($json, true). At authoring time, when the data belongs in a config file, a Laravel seeder or a PHPUnit fixture, you want the literal array. This converter writes it using the short [] syntax introduced in PHP 5.4, with objects as associative arrays and JSON arrays as list arrays.

Strings are single-quoted with backslashes and apostrophes escaped, so no variable interpolation can occur. Numbers, booleans and null are written as PHP literals. The result is wrapped in <?php and assigned to $data; rename the variable or drop the tag when pasting into an existing file. The default indent is four spaces, matching PSR-12.

The converter preserves key order, which PHP associative arrays also preserve, so iterating the array yields the same sequence as the JSON. Trailing commas are not emitted after the last element to keep the output compatible with older code style checkers.

How to use

  1. Paste JSON on the left.
  2. Choose the indent used by your project (PSR-12 is 4 spaces).
  3. Copy the array and paste it into your PHP file.

Common questions

Why single quotes for strings?
Single-quoted PHP strings do not interpolate variables or process escape sequences other than backslash and quote, so the value is reproduced exactly.
Are JSON objects converted to associative arrays or stdClass?
Associative arrays, the same as json_decode($json, true). That is what config files and fixtures normally use.
Is the output valid for PHP 7 and 8?
Yes. The short array syntax works from PHP 5.4 onward.