JSON to PHP array
Turn JSON into a PHP array literal you can paste straight into a script — switch between [ ] short syntax and array( ) long syntax, pretty or compact output, with PHP single-quote escaping.
Runs in your browserEvery computation happens in your browser — your data never leaves this device.
The text must pass JSON.parse; objects become associative arrays, arrays become list arrays and keys keep their original names.
PHP code
The output ends in a $data variable — rename it and paste the code into your script.
Enter JSON to see the PHP code
Top-level keys: 0 · Code lines: 0
What this tool does
- Turn a JSON API response or config into a PHP array literal without hand-typing quotes and arrows
- Generate fixture data for PHP unit tests
- Migrate a legacy JSON config into a PHP file using array( ) syntax
Example
Input
{"name":"Ada","active":true}Output
<?php
$data = [
'name' => 'Ada',
'active' => true
];
Defaults are short-array syntax with pretty printing: the object becomes an associative array, strings use single quotes and the boolean stays true.
Frequently asked questions
What is the difference between the array syntaxes?
Short syntax [ ] is available since PHP 5.4 and is the modern recommendation; array( ) is the long form kept for older versions. They are semantically identical — switching only changes the text. Empty containers render as [] or array() respectively.
How are strings escaped?
They use PHP single-quoted strings, escaping only \ and ' (\ becomes \\ and ' becomes \'). Everything else — Chinese characters, newlines — goes into the string literally. Single-quoted strings do not expand $variables or escapes such as \n, so PHP will not reinterpret the content.
Do JSON numbers map exactly to PHP numbers?
JSON has a single number type: an integer without a decimal point inside the safe range is emitted as a PHP int, while fractions or exponents become floats. Very large integers are already approximated as IEEE 754 doubles by JSON.parse, so keep identifiers such as order numbers as strings if you need them exact.
Can I change the variable name?
The output ends in a $data assignment — just rename it, or wrap the literal in return [...] inside a function. The tool only produces code text: it never talks to a PHP runtime and never writes a file.
Does it generate an array or a class, and which types are supported?
Only a PHP array literal — no class, DTO, namespace or file. A JSON object becomes an associative array (keys as single-quoted strings), a JSON array becomes a sequential array, strings become single-quoted strings, integers become ints, fractions or exponents become floats, and true/false/null are emitted as true/false/null. Nested values follow the same rules at every level.
Keywords:json2phpphp arrayshort array syntax