PHP Array to JSON
Paste a PHP array, from a config file, a var_export() dump or even print_r() output, and get the JSON that json_encode would produce. The converter understands short and long array syntax, implicit and explicit keys, PHP’s key-casting rules, single- and double-quoted strings, numeric formats with underscores and comments, and it mirrors the common json_encode flags. Nothing is executed: the code is parsed, never run.
- Runs in your browser
- No sign-up
- Free to use
How to use PHP Array to JSON
- Paste the PHP array, with or without <?php, return or a variable.
- Tick the json_encode flags you use.
- Check the notes about empty arrays and print_r input.
- Copy or download the JSON.
PHP Array to JSON features
Three input forms
PHP code, var_export() output and print_r() output.
PHP semantics
Implicit keys, numeric-string keys, duplicate keys and list detection like PHP.
json_encode flags
PRETTY_PRINT, UNESCAPED_SLASHES, UNESCAPED_UNICODE, FORCE_OBJECT, PRESERVE_ZERO_FRACTION.
Full string support
Single and double quotes, escapes, octal, hex and \u{…}.
Safe
No PHP is run; variables, constants and functions are reported.
Local
Works in your browser.
When to use PHP Array to JSON
- Moving configuration from a PHP file into JSON for a front end or another service.
- Turning a var_dump-style debug print into usable data.
- Checking whether a PHP array encodes as a JSON array or an object.
- Producing API fixtures from PHP test data.
PHP Array to JSON FAQ
When does a PHP array become a JSON array?
When its keys are exactly 0, 1, 2 … in order. Any other keys, or a gap, make json_encode output an object. JSON_FORCE_OBJECT always produces objects.
Why did my "1" key become 1?
PHP converts string keys that look like decimal integers into integer keys. The converter applies the same rule, which affects whether the result is a list.
Can it read print_r output?
Yes, but print_r loses types: true prints as 1 and false and null as empty strings. Prefer var_export(), whose output is valid PHP and keeps types.
Why are slashes escaped as \/?
json_encode escapes forward slashes by default. Tick JSON_UNESCAPED_SLASHES to leave them alone, as most code does.
What about constants like PHP_EOL?
Their values depend on the PHP installation, so they cannot be converted without running PHP. Replace them with literal values.
Is PHP executed on the server?
No. The array is parsed in your browser by a JavaScript parser that only understands literal values.
How PHP arrays map to JSON
PHP has one array type for both lists and dictionaries, while JSON has two: arrays and objects. json_encode decides per array. If the keys are the consecutive integers 0, 1, 2 and so on, starting at zero and in order, it writes a JSON array; otherwise it writes an object with the keys as strings. An empty array is written as [] unless JSON_FORCE_OBJECT is used, which is a frequent source of bugs when the other side expects {}.
Keys follow PHP’s casting rules. Strings that look like canonical decimal integers become integer keys, booleans become 0 or 1, null becomes the empty string, and floats are truncated. Values without an explicit key receive the next integer after the highest one used so far. The converter applies all of these rules, so the JSON matches what PHP itself would produce.
Strings deserve attention too. Single-quoted strings only understand \ and ' escapes, while double-quoted strings support \n, \t, \x, \u{…}, octal escapes and variable interpolation. A double-quoted string containing $name depends on a variable that only exists when PHP runs, so the converter reports it instead of guessing. Constants and function calls are reported for the same reason.
The flags change the formatting of the output. JSON_PRETTY_PRINT indents with four spaces as PHP does, JSON_UNESCAPED_SLASHES stops the \/ escaping that PHP applies by default, and JSON_UNESCAPED_UNICODE writes characters such as é directly instead of as \u00e9. JSON_PRESERVE_ZERO_FRACTION keeps 1.0 as 1.0 rather than 1, which matters when the receiving side distinguishes integers from floats.
Debug output can be converted as well. var_export() produces valid PHP and converts exactly. print_r() is more readable for humans but loses type information, so the converter guesses numbers and notes that booleans and null cannot be recovered. For anything important, re-export the data with var_export() or json_encode() in PHP itself.