Skip to content
100% local

PHP array to JSON

Convert a PHP array() or [] literal to JSON, and back again.

Input
Output

PHP array to JSON

Paste a PHP array written with array(...) or short [...] syntax and this tool converts it to JSON with the correct types — strings, numbers, booleans and null are all preserved rather than turned into strings. It also runs in reverse: paste JSON and generate a PHP array literal you can drop straight into your code, whether you are debugging a var_export() dump, migrating a config file, or turning an API response into a fixture for a test.

The Direction option switches between PHP → JSON and JSON → PHP, and the swap button carries the current output back into the input so you can round-trip a value and check it survives the conversion intact. When generating PHP, Use short array syntax chooses between [...] and the older array(...) form; Preserve numeric keys keeps every index explicit instead of letting a plain 0..n-1 list collapse into an implicit one; null / true / false case picks lowercase or UPPERCASE constants; and Escape apostrophes switches generated strings between single quotes (with \' escaped) and double quotes. Pretty print and Indent size control whitespace, and Sort keys orders object and associative-array keys alphabetically at every nesting level.

Parsing a PHP array understands single- and double-quoted strings with their different escaping rules, integers, floats, negative numbers, nested arrays, comments, and trailing commas. A sequential array with keys 0, 1, 2… in order becomes a JSON array; anything else — string keys, gaps, or a key that resets the counter — becomes a JSON object, as PHP's own json_encode() would treat it. Windows and Unix line endings are handled the same way, and the live tally reports the item count, line count and character count as you type.

Everything runs locally in your browser — the array you paste is never uploaded anywhere. When you're done, copy the result, download it as a .txt file, or send the output back into the input to run it through the tool again in the other direction.

FAQ

Does it handle nested arrays?
Yes. Arrays and associative arrays can nest to any depth in both directions, and options like Sort keys and Preserve numeric keys apply at every level.
How does it decide whether an array becomes a JSON array or an object?
A PHP array whose keys run 0, 1, 2… in order becomes a JSON array. Anything else — string keys, gaps in the numbering, or Preserve numeric keys turned on — becomes a JSON object with the keys written out as strings.
Can I generate old-style array() syntax instead of [ ]?
Yes. Turn off Use short array syntax when converting JSON to PHP and the output uses array(...) instead of [...].
What happens with invalid PHP or JSON?
The tool reports the line and column where parsing failed and what it expected there, instead of guessing at a result.
Is my code uploaded anywhere?
No. The conversion runs entirely in your browser — nothing you paste is sent to a server.