PHP Array to JSON Converter
Convert PHP arrays to JSON and JSON to PHP arrays online
All conversions are done locally in your browser. Your data is never sent to any server.
In-depth guide
PHP arrays and JSON: how they map, and what this converter really reads
A tested account of this page: the text-rewriting steps behind PHP to JSON, which inputs convert and which fail, the escaping hazards in JSON to PHP output, and the PHP-side rules (json_encode, json_decode, var_export) to use when exactness matters.
Key figures at a glance
- PHP to JSON input that works
- Short-syntax lists and JSON-like text
- For example ['a','b'], [1,2,3,] and [[1,2],[3]]. Plain JSON text also passes through.
- PHP to JSON input that fails
- Anything with => or array( ... )
- The page swaps => for a colon and array( for [ but never the closing bracket, so key/value arrays and array() are rejected as invalid.
- JSON to PHP output
- Double-quoted strings, 4-space indent, => for objects
- Only the double quote is escaped in strings; a $ or backslash in the data is not.
- var_export option
- Changes nothing
- Output is identical whether the box is ticked or not, and it is not var_export's real format.
Converting a PHP array to JSON reliably
Let PHP do the PHP parsing; use the page for the JSON side.
-
Step 1: Encode in PHP
Run json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) in your own script or a REPL.
-
Step 2: Check the JSON
Paste the result into the validator to confirm it parses and to see any position-specific error.
-
Step 3: Reformat if you need a different style
Use the formatter for 4-space or tab indentation, or the minifier for one line.
What this page really does
Neither direction uses a PHP parser. Both run in your browser and neither uploads anything, but the PHP to JSON direction is a short chain of text substitutions followed by JSON.parse:
- Trim the text and delete any
<?phpand?>. - Replace every
array(with[. The matching)is not touched. - Replace every
=>with:. - Replace every single quote
'with a double quote". - Delete commas that sit before a closing
]or}. - Parse as JSON and print it, pretty (2 spaces) or compact.
Whatever survives that pipeline as valid JSON converts; everything else fails with "Conversion error: Invalid PHP array syntax: …" followed by the browser's JSON message. After a failure the output box is not cleared, so it can still show the result of an earlier run. Read the red status line, not the box.
What converts and what does not
Results below were obtained by running the page.
| PHP input | Result |
|---|---|
['a','b','c'] |
works: ["a","b","c"] |
[1, 2, 3,] |
works: [1,2,3] (trailing comma removed) |
[true, false, null] |
works |
[[1,2],[3]] |
works |
{"a":1} (already JSON) |
works: nothing needs rewriting |
['name'=>'Mei','age'=>30] |
fails: becomes ["name":"Mei","age":30], which is not JSON |
array('a','b') |
fails: becomes ["a","b") |
['it\'s'] |
converts, but wrongly: the result is it"s |
["it's"] |
fails: the apostrophe becomes an unbalanced quote |
[TRUE, NULL] |
fails: PHP constants are case-insensitive, JSON literals are not |
$x = [1];, return [1,2];, [1,2]; // note |
fails: statements, semicolons and comments are not understood |
[1=>'a', 2=>'b'], [0x1F, 010] |
fails |
In practice the PHP to JSON direction handles only plain lists and JSON-looking text. The page's own description and FAQ claim more than this; do not rely on it for associative arrays.
JSON to PHP output
This direction parses JSON properly, then prints PHP source. Verified output for {"a":1,"b":[1,2],"c":{"d":null}}:
[
"a" => 1,
"b" => [
1,
2
],
"c" => [
"d" => null
]
]
JSON arrays become lists, JSON objects become "key" => value, and {} becomes []. Untick short syntax to get array( ... ). There is no $var = prefix, no trailing semicolon and no trailing commas, so add those yourself. Three hazards come from writing strings in PHP double quotes with only " escaped:
- Variable interpolation: the JSON string
"cost: $price"becomes"cost: $price"in PHP, where$priceis replaced by a variable's value (or an undefined-variable warning). Likewise{$x}. - Backslashes: the JSON value
back\nslash(a real backslash followed by n, written"back\\nslash"in JSON) is printed as"back\nslash", which PHP reads as a newline. Raw line breaks are copied literally, which is fine. - Keys are quoted but not escaped, so a key containing
"produces invalid PHP.
Numbers use JavaScript's formatting: 1.0 becomes 1 (an int, not a float), 1e21 becomes 1e+21, 12345678901234567890 becomes 12345678901234567000. The var_export checkbox changes nothing. Real var_export prints single-quoted strings, array ( with a space, explicit keys for lists (0 => 1,), NULL in capitals and trailing commas.
The PHP-side rules worth knowing
Checked with PHP 8.3:
| PHP | JSON |
|---|---|
[1,2,3] |
[1,2,3] |
['a'=>1] |
{"a":1} |
[1=>'a',2=>'b'] |
{"1":"a","2":"b"} (keys not 0..n-1) |
[0=>'a',1=>'b'] |
["a","b"] (sequential keys make a list) |
[] |
[] (an empty array is never {}; use new stdClass for that) |
1.0 |
1, or 1.0 with JSON_PRESERVE_ZERO_FRACTION |
"a/b é" |
"a\/b é" by default; "a/b é" with JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
json_decode($json, true) returns associative arrays; without true it returns stdClass objects. {"1":"a","2":"b"} decodes to an array with integer keys 1 and 2, and a JSON integer above PHP_INT_MAX such as 12345678901234567890 decodes to a float. A trailing comma makes json_decode return NULL with the error "Syntax error".
Tip: For anything you will commit or run, produce PHP source with
var_export($data, true)in PHP itself, and JSON withjson_encode. Use this page for quick lists and for eyeballing structure.
Common mistakes
- Pasting
array('key' => 'value')or['key' => 'value']and being surprised by the error. - Trusting the output box after an error; it may be from the previous conversion.
- Pasting JSON-to-PHP output into code without checking strings containing
$or backslashes. - Expecting
1.0or[]versus{}to survive a round trip.
Related tools
Validate or reshape the JSON with the JSON validator, JSON formatter or JSON viewer, and flatten it to paths with JSON to text.
Frequently Asked Questions
How do I convert a PHP array to JSON?
Select "PHP Array to JSON" mode, paste your PHP array code (using either [] or array() syntax), choose your JSON format preference (pretty or compact), and click Convert. The tool will instantly convert your PHP array to valid JSON format.
Can I convert JSON back to a PHP array?
Yes! Select "JSON to PHP Array" mode, paste your JSON data, choose your preferred PHP syntax (short [] or traditional array()), and click Convert. The tool supports bidirectional conversion.
What's the difference between short and traditional PHP array syntax?
Short syntax uses brackets [] and is available in PHP 5.4+, while traditional syntax uses array(). For example: ["a" => 1] versus array("a" => 1). Both are functionally identical, but short syntax is more concise and widely used in modern PHP.
Does the tool handle nested arrays?
Yes, the converter fully supports nested arrays and objects of any depth. It correctly handles complex data structures with multiple levels of nesting.
Are data types preserved during conversion?
Yes, the tool preserves strings, numbers, booleans (true/false), and null values during conversion. Associative arrays become JSON objects, and numeric indexed arrays become JSON arrays.
Is my data sent to a server?
No, all conversions happen entirely in your browser. Your data never leaves your device, making this tool completely safe for sensitive information like API keys or configuration data.
What is var_export format?
var_export is a PHP function that outputs a parsable string representation of a variable. This option generates PHP code in the exact format produced by PHP's var_export() function, which is useful for code generation and debugging.
Can I use this for large JSON or PHP arrays?
Yes, the tool can handle large data structures. However, very large files (several megabytes) may take longer to process depending on your browser and device performance.
What happens if my PHP array or JSON is invalid?
The tool will validate your input and show specific error messages indicating what went wrong, helping you quickly identify and fix syntax errors.
Can I download the converted output?
Yes, use the Download button to save the converted output as a .json file (for JSON output) or .php file (for PHP array output).
Why would I need to convert between PHP and JSON?
Common use cases include: converting API responses for use in PHP applications, generating PHP test data from JSON fixtures, creating configuration files, data migration between systems, and documentation examples.
Does pretty-printed JSON affect the data?
No, pretty-printing only adds whitespace (spaces and newlines) for readability. The actual data structure and values remain identical to compact JSON. The only difference is file size and human readability.
Sources
- PHP manual — json_encode — Lists versus objects, escaping flags, float handling.
- PHP manual — json_decode — Objects as stdClass or associative arrays, big integers.
- PHP manual — var_export — The real output format of var_export.
- PHP manual — Strings — Variable interpolation and escape sequences in double-quoted strings.
- IETF — RFC 8259, The JSON Data Interchange Format
This guide explains published rules in plain language. It is not financial, legal or official advice — rules change, so confirm with the agency linked above before you commit.
Embed this tool on your website
Free to use. Paste this code into your page and keep the link back to ToolSG.