Xerobit

JSON to PHP Array Converter

Paste a JSON string to convert it into valid PHP array syntax. The tool handles nested objects, arrays, numbers, booleans and null values, producing code you can drop straight into a PHP file. It runs entirely in your browser and nothing is uploaded to any server.

Paste valid JSON here.

How many spaces or tabs per nesting level.

PHP Array

[
    'name' => 'Alice',
    'age' => 30,
]

How to use the json to php array converter

  1. Paste your JSON text into the JSON input field.
  2. Choose your preferred indentation: 2 spaces, 4 spaces or tab.
  3. The PHP array output updates automatically as you type.
  4. Copy the generated PHP code and paste it into your project.

How the conversion works

The converter parses the JSON string into native JavaScript values, then walks the resulting structure recursively to emit PHP short array syntax. JSON objects become associative arrays using the [key => value] form, while JSON arrays become indexed arrays. Strings are wrapped in single quotes with internal single quotes escaped as \'. Booleans map directly to PHP true and false, and JSON null becomes PHP null.

For example, the JSON input {"user": "alice", "active": true} produces the following PHP code:

[ 'user' => 'alice', 'active' => true, ]

This recursive approach means that no matter how deeply nested your original data is, every level translates cleanly into matching PHP syntax without losing structural information.

Type mapping reference

Every JSON data type has a direct equivalent in PHP. The table below shows how each type is translated during the json to php array conversion.

JSON typePHP outputExample
ObjectAssociative array{"a":1} becomes ['a' => 1]
ArrayIndexed array[1,2] becomes [1, 2]
StringSingle-quoted string"hi" becomes 'hi'
NumberInteger or float42 stays 42, 3.14 stays 3.14
Booleantrue / falsetrue becomes true
Nullnullnull becomes null

Understanding these mappings helps you predict exactly what the output will look like before you even run the conversion, which is especially useful when debugging configuration files or API responses.

Handling special characters in strings

When converting JSON strings to PHP, the tool uses single quotes because they prevent accidental variable interpolation in PHP. Any single quote inside the original JSON string is escaped with a backslash. A string that contains a control character such as a newline, tab or NUL is written in double quotes with PHP escapes ( , , ) so the value stays exact. Numbers are copied exactly as written. Double quotes from the JSON do not need escaping inside PHP single-quoted strings, so they pass through unchanged.

Backslashes themselves are preserved. A JSON string like "path\\to\\file" becomes 'path\\to\\file' in PHP, which evaluates to the same path at runtime. This careful escaping ensures that the converted code behaves identically to the original JSON payload when executed by the PHP interpreter.

Trailing commas in PHP arrays

The generated output includes a trailing comma after the last element in every array. While older PHP versions rejected this, PHP 7.3 and later accept trailing commas in array literals, function calls and parameter lists. Trailing commas make version control diffs cleaner because adding a new element only changes one line instead of two.

If you need to support PHP 7.2 or earlier, simply remove the final comma before the closing bracket in each array block. Most modern frameworks and content management systems already require PHP 8.0 or higher, so trailing commas are rarely an issue in practice.

Working with other formats

Sometimes your source data is not JSON. If you have CSV data, use the csv to array tool first, then format it as JSON before converting. For configuration files, tools like toml to json, ini to json or env to json can normalize the input. Once you have valid JSON, this converter handles the rest.

If you need to go the other direction, the php array to json tool reverses the process. Chaining these utilities together lets you move data between completely different ecosystems without writing custom parsing scripts.

Worked examples

ExampleInputResult
Simple objectJSON input: {"name":"Alice","age":30}; Indentation: 4[ / 'name' => 'Alice', / 'age' => 30, / ]
Nested array of objectsJSON input: {"users":[{"id":1,"active":true},{"id":2,"active":false}]}; Indentation: 4[ / 'users' => [ / [ / 'id' => 1, / 'active' => true, / ], / [ / 'id' => 2, / 'active' => false, / ], / ], / ]
Mixed types including nullJSON input: {"count":0,"label":null,"tags":["php","json"]}; Indentation: 2[ / 'count' => 0, / 'label' => null, / 'tags' => [ / 'php', / 'json', / ], / ]

FAQ

Does this handle deeply nested JSON?

Yes. The converter uses recursion, so objects and arrays nested to any depth are translated correctly into matching PHP array structures.

Why does the output use single quotes for strings?

Single quotes are the standard convention for literal strings in PHP. They avoid accidental variable interpolation and require less escaping than double quotes.

Can I convert a JSON array without an outer object?

Yes. A top-level JSON array like [1, 2, 3] converts directly to a PHP indexed array [1, 2, 3].

Is my data sent to a server?

No. The json to php array conversion happens entirely in your browser using JavaScript. Nothing is uploaded anywhere.

What happens if my JSON is invalid?

The tool displays a parse error message indicating where the JSON syntax is broken. Fix the JSON input and the PHP output will appear automatically.

Are numeric keys preserved?

JSON object keys are always strings. In the PHP output, numeric-looking keys such as "0" are kept as quoted strings to preserve the exact structure of the original JSON.