Skip to content
gohacks.dev
Standard Library

PHP Array to JSON Converter

Convert a PHP array literal, a Laravel config file or var_export() output to JSON, with short and long array syntax and nested arrays of any depth.

Free · No sign-up · Your inputs stay in your browser

Input

Convert a PHP array to JSON

Output

JSON

241 characters

Short [ ] and long array( ) syntax, nested to any depth, with true, false and null in any case. A sequential array from 0 becomes a JSON list and anything else an object, the same rule json_encode() uses. Function calls such as env() cannot be evaluated: replace them with their values first.

Runs in this browser. Nothing you paste is sent to a server.

Lists versus maps

PHP has one array type for both, so the converter has to decide, and it decides like json_encode(): sequential integer keys from zero make a list, anything else makes an object. An array with keys 1, 2, 3 therefore becomes an object, which surprises people converting data that was filtered with array_filter(); array_values() re-indexes it.

Function calls are not evaluated

Laravel config files call env() and helpers such as storage_path(). Those calls cannot be resolved outside the application, so replace them with their values, or run php artisan config:show app --json, which prints a resolved section of the configuration.

Share

Frequently asked questions

How do I convert a PHP array to JSON in PHP itself?
json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR). The flags make the output readable and turn encoding failures into exceptions instead of a false return value.
Does it read var_export() output?
Yes. var_export() writes long array( ) syntax with quoted keys, which the converter reads as it does a hand-written array.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Go deeper

Suggested reading

Keep planning

Similar tools