Skip to content
Convert Filez
Data And FormatsRuns in your browser6 min readUpdated July 29, 2026

JSON to PHP Array Converter

This JSON to PHP array converter reads a JSON value and writes back a valid PHP array literal you can paste directly into a script, a configuration file or a test fixture. Paste JSON or drop a .json file, pick short array syntax or the older long form, and get PHP code that runs exactly as written with no manual retyping.

Loading the tool interface
Files never leave your deviceNo upload wait and no queueNo signup, watermark or file limit

PHP has no native JSON literal syntax the way JavaScript does, so a JSON object has to become a PHP associative array built with the => operator, and a JSON array has to become a PHP array with implicit integer keys. Typing that translation by hand for anything beyond a tiny sample means constantly getting a comma, a quote or a nesting level wrong somewhere in the middle of a long structure.

The conversion runs entirely in your browser using JSON.parse and a small recursive writer, so nothing you paste or upload is sent anywhere. A configuration sample pulled from a production system, which is exactly the kind of thing you would want turned into PHP for a script or a test, never has to leave your machine to make that trip.

When you should convert JSON to PHP array literals

Converting is worth doing whenever a JSON sample, whether it came from an API response, a config file or a JavaScript test fixture, needs to exist as native PHP data rather than being decoded at runtime with json_decode. That comes up constantly when writing a PHPUnit test fixture, seeding a default configuration array, or hard coding a small lookup table straight into a PHP file.

It is also useful any time you are porting a project from a JavaScript or Node based prototype into a PHP codebase and the sample data used during prototyping should travel with it as native PHP rather than a JSON string parsed on every request.

  • Convert to PHP for a PHPUnit test fixture or a mock data array.
  • Convert to PHP to hard code a default configuration directly into a script.
  • Convert to PHP when porting JavaScript prototype data into a PHP codebase.
  • Keep JSON when the data is read from an external file or API at runtime with json_decode.

How this json to php array converter uses the => operator

Every JSON object becomes a PHP associative array, and PHP represents an associative array's key and value pairs with the => operator, so a JSON object like {"role": "admin"} becomes the PHP expression 'role' => 'admin' inside the surrounding array literal. This is the same rule that turns a json to associative array conversion into working code rather than a syntax error, since PHP has no other literal way to express a string keyed mapping. Each key is written as a single quoted PHP string, since single quotes in PHP do not interpret escape sequences or variable interpolation, which is exactly the safe, literal behaviour a converted string value needs.

A JSON array becomes a plain PHP array with no explicit keys at all, since PHP assigns sequential integer keys automatically starting from zero when none are given, matching how the original JSON array was ordered.

Escaping strings correctly for a php array literal generator

A single quote inside a PHP single quoted string has to be escaped as \' or PHP will read it as the end of the string, and a literal backslash has to be escaped as \\ for the same reason. This php array literal generator applies both escapes automatically, so a value containing an apostrophe, such as a name like O'Brien, converts to a string PHP parses correctly rather than one that breaks the surrounding array on the first apostrophe it hits.

Numbers, the JSON literals true, false and null all map onto PHP's own true, false and null keywords rather than being wrapped in quotes, since writing them as strings would silently change their type the moment PHP evaluated the array, turning a boolean check or a numeric comparison further down the code into something that no longer behaves the way the original JSON value did.

Choosing short array syntax or the classic array() form

PHP has supported the short array syntax using square brackets since version 5.4, and it is what almost every modern codebase and style guide now uses by default, since it is shorter to type and read than the older form. This converter defaults to it, producing output like ['role' => 'admin'] for a JSON object.

Switching to the long form instead produces array('role' => 'admin'), which remains valid PHP in every supported version and is still what some legacy codebases and older style guides require. Both forms are functionally identical once PHP parses them, so the choice here is purely about matching the conventions of the codebase the output is going into.

How this converter builds nested PHP arrays

Nesting in the source JSON is preserved exactly: a JSON object nested inside another object's value becomes a nested PHP associative array written inline, and the same applies to an array nested inside an object or another array. Indentation increases with each level of nesting, so a deeply nested configuration sample stays readable rather than collapsing onto one impossibly long line.

Because the whole conversion is a straightforward recursive walk over the value JSON.parse already produced, there is no size limit imposed by the tool itself beyond your browser tab's own memory, and a malformed JSON sample is reported with the exact character position where parsing stopped so the source can be fixed before trying again. That makes it a practical json to php online step for a one off conversion rather than a library you need to install just to try it once.

How to convert JSON to a PHP array

  1. 1

    Add your JSON

    Paste a JSON value into the box, or drop a .json file. Nothing leaves your device.

  2. 2

    Choose array syntax

    Pick short array syntax with square brackets, or the classic array() form for older codebases.

  3. 3

    Set the indent width

    Match the indentation used elsewhere in the PHP file this code is going to be pasted into.

  4. 4

    Convert and copy

    Press convert, review the generated PHP, then copy it or download it as a .php file.

Related tools worth bookmarking

Sources and further reading

Frequently asked questions

Common questions about the json to php array converter.

No, because array keys in PHP are just strings inside the => operator rather than variable names, so a key containing a space, a hyphen or a number at the start converts safely as 'order-total' => 42 without needing any special handling. This only matters if you later destructure the array into individual variables in your own code.

Yes. Every JSON object becomes a PHP associative array using the => operator to pair each key with its value, exactly the syntax PHP requires for an associative array, while a JSON array becomes a plain PHP array with implicit integer keys instead.

A single quote inside a string value is escaped as \' and a literal backslash is escaped as \\, matching the rules PHP requires inside a single quoted string literal. This keeps a name like O'Brien or a Windows style file path from breaking the generated array.

Yes. A toggle switches between the short array syntax introduced in PHP 5.4, which almost all modern code uses, and the classic array() form some legacy codebases and older style guides still require. Both are functionally identical once PHP parses them.

They convert directly to PHP's own true, false and null keywords rather than being wrapped in quotes as strings, since quoting them would silently change their type and could break a boolean check or comparison further down in the code that consumes the array.

No. JSON.parse builds the JavaScript value first, and the recursive writer that emits each => pair and escapes every string with PHP's own quoting rules runs entirely against that value inside your browser tab, so a configuration sample containing real credentials never leaves your device during the conversion.

Keep going

More data and formats

View the full category