>_devtools

JSON to PHP Array Converter

Convert JSON to a PHP array literal in short or array() syntax, optionally as a return statement or $data assignment.

What this does

Converts JSON into a PHP array literal you can paste into a config file, fixture, or test. JSON objects become associative arrays ('key' => value) and JSON arrays become list arrays.

Example

Input

{
  "id": 42,
  "name": "Ada",
  "verified": true,
  "tags": [
    "admin",
    "dev"
  ],
  "orders": [
    {
      "sku": "A1",
      "qty": 2
    },
    {
      "sku": "B2",
      "qty": 1,
      "note": "gift"
    }
  ]
}

Output with default options

[
    'id' => 42,
    'name' => 'Ada',
    'verified' => true,
    'tags' => [
        'admin',
        'dev',
    ],
    'orders' => [
        [
            'sku' => 'A1',
            'qty' => 2,
        ],
        [
            'sku' => 'B2',
            'qty' => 1,
            'note' => 'gift',
        ],
    ],
]

Options

  • Syntax: short [ ... ] (PHP 5.4+) or the long array( ... ) form.
  • Wrap as: a bare expression, a return statement (the format of Laravel and Symfony config files), or a $data = assignment. The last two include the <?php open tag.
  • Indent and trailing commas, matching PSR-12 style by default.

Details

Strings use single quotes, with only \ and ' escaped, so$ and \n are not interpreted by PHP. null,true, and false are written as PHP literals, and empty objects and arrays both become an empty array. Numbers keep their JSON form. PHP converts numeric-string keys such as '123' to integers when it builds an array, which is standard PHP behavior and not something this tool changes.

Privacy

The conversion runs in your browser. The JSON is never uploaded.