FormatterHQ

JSON to PHP Array Converter

Turn JSON into ready-to-paste PHP array code: objects become associative arrays, with short [ ] or classic array( ) syntax, your own variable name and indentation.

Input JSON
Output PHP
Ready.

How to use the JSON to PHP Array Converter

  1. Paste your JSON, upload a .json file, or click Sample.
  2. Choose the Syntax (short [ ] or array( )), set the Variable name and decide whether to Include <?php tag.
  3. Pick an Indent; 4 spaces is the default, following the common PHP coding style.
  4. Click Convert to PHP and copy or download the code.

JSON to PHP array: generate code from data

Sometimes you want data in PHP source rather than in a JSON file: a config array, a seeder, a fixture for a PHPUnit test, or a lookup table. This JSON to PHP array converter writes that code for you, with correct quoting and nesting, so you can paste it straight into a .php file.

Example

{
  "name": "O'Brien",
  "price": 9.5,
  "active": true,
  "manager": null,
  "tags": ["php", "json"],
  "meta": {},
  "200": "OK"
}

With the default options:

<?php

$data = [
    200 => 'OK',
    'name' => 'O\'Brien',
    'price' => 9.5,
    'active' => true,
    'manager' => null,
    'tags' => [
        'php',
        'json',
    ],
    'meta' => [],
];

How JSON maps to PHP

JSONPHP output
ObjectAssociative array with 'key' => value pairs
ArrayList without explicit keys
StringSingle-quoted string; ' and \ are escaped
NumberInteger or float literal
true, false, nulltrue, false, null
Key like "200"Unquoted integer key 200

Single-quoted strings were chosen on purpose: PHP does not interpolate $variables or interpret \n inside them, so text such as "Price: $5" is reproduced literally. Every element, including the last, is followed by a comma, which PHP allows and which keeps diffs small.

Options

  • Syntax – Short [ ] is the modern style available since PHP 5.4. array( ) produces the classic long form for older code bases or style guides that require it.
  • Variable name – defaults to data. Characters other than letters, digits and underscores are removed, so my-config becomes $myconfig.
  • Include <?php tag – on by default. Turn it off when you paste into an existing file.
  • Indent – 4 spaces by default, or 2, 3 spaces or a tab.

Details to check before you paste

  • Integer keys move to the front. In the example, 200 => 'OK' appears first although it was last in the JSON. The conversion runs in JavaScript, which lists integer-like keys before other keys. Keys such as "01" are not integers and stay quoted in place.
  • Empty objects become empty arrays. PHP has one array type, so {} and [] both become []. If you later call json_encode on the array, it produces [], not {}.
  • Whole-number floats lose the decimal. 10.0 is written as 10, which PHP reads as an integer.
  • Line breaks in values are written as real line breaks inside the quoted string. That is valid PHP but spreads the string over several lines.

Using the generated array

A common pattern is a config file that returns the array directly. Keep the <?php tag, replace $data = with return, and the file can be loaded with $config = require 'config.php';. For test fixtures, keep the variable and include the file in your test case, or paste the array into a data provider method.

Static array or json_decode?

If the JSON arrives at runtime, from an API or a file that changes, keep it as JSON and call json_decode($json, true), which returns the same associative array structure. Generate static code only for data that belongs in the source. If you want typed PHP classes instead of arrays, use JSON to PHP classes. To tidy existing PHP, try the PHP formatter, and for XML input there is XML to PHP.

The conversion runs in your browser; your JSON is not uploaded unless you use Load URL or Save & Share.

Frequently asked questions

How do I convert JSON to an array in PHP code?
At runtime, call json_decode($json, true); the second argument returns associative arrays instead of objects. To embed the data in source code instead, paste the JSON here and copy the generated array.
Should I use short [ ] or array( ) syntax?
Short syntax works in every PHP version since 5.4 and is the common choice today. Use array( ) only if your project still supports very old PHP or its coding standard requires it.
Why did the order of my keys change?
Numeric keys such as "200" are placed before other keys, because the JavaScript object that holds the parsed JSON lists integer keys first. Other keys keep their original order.
Are dollar signs in strings safe?
Yes. Strings are written in single quotes, where PHP does not expand variables, so a value like "$HOME" stays exactly as it is.
Why is an empty JSON object converted to []?
PHP uses arrays for both lists and maps, so an empty object and an empty list look the same. If you need {} when encoding back to JSON, use new stdClass() or an ArrayObject for that value.

Last updated October 3, 2026

Guides

Related tools

JSON to PHP Generate PHP classes with typed properties from a JSON sample instantly, with nested types detected automatically. cURL to PHP Converter Convert curl commands into PHP code using the curl_* functions: method, headers, body, auth, cookies and SSL options. HTML to PHP Converter Convert HTML into PHP echo statements, a string variable or a heredoc block with every quote and dollar sign safely escaped. JSON to String Converter Convert JSON to a single escaped string you can paste into code, databases or other JSON documents. Choose double, single or no quotes. JSON Tree Viewer Display JSON as an expandable tree. Collapse and expand objects and arrays to understand deeply nested data structures. JSON Unflatten Rebuild nested JSON from flat dot-notation keys (a.b.c) or bracket paths (a[0].b), the reverse of JSON flatten. JSON Validator Validate JSON online and find syntax errors fast. Get the exact line and column of each problem and fix broken JSON in one click. JSON Viewer & Beautifier View JSON as a collapsible tree and beautify it at the same time. Explore nested objects and arrays without losing your place. JSON5 Formatter Format and beautify JSON5 with comments, unquoted keys and trailing commas, or convert JSON5 to strict JSON. JSON5 Validator Validate JSON5 syntax online. Check config files with comments and trailing commas and get line and column for each error. JSONL to JSON Converter Convert JSON Lines (JSONL / NDJSON) into a formatted JSON array. Invalid lines are reported with their line number. JSONPath Tester Test JSONPath expressions online against your JSON. Supports filters, wildcards, recursive descent and slices, returning values or paths.