PHP Array to JSON Converter — Free Online
PHP Array to JSON Converter
Transform any PHP array syntax into valid JSON. Supports both short ([...]) and long (array(...)) array syntax. Handles associative arrays with string keys, indexed arrays, nested arrays, and mixed types. Booleans are parsed from true/false, null from null. The output is properly indented JSON.
Use Cases
- Config Migration — Convert PHP config files to JSON format
- API Development — Transform PHP array responses to JSON payloads
- Data Portability — Move from PHP-based config to JSON-based config
- Cross-Language — Share PHP data structures with non-PHP applications
Example
Input:
<?php
return [
'app' => [
'name' => 'MyApp',
'version' => '1.0.0',
'debug' => false,
'servers' => ['192.168.1.1', '192.168.1.2'],
'database' => [
'host' => 'localhost',
'port' => 5432,
],
],
];Output:
{
"app": {
"name": "MyApp",
"version": "1.0.0",
"debug": false,
"servers": ["192.168.1.1", "192.168.1.2"],
"database": {
"host": "localhost",
"port": 5432
}
}
}Edge cases and how they are handled
- Both syntaxes work. The short
[...]form and the<?php return …;wrapper are handled. - Comments are removed.
//line comments and/* … */block comments are stripped before parsing. - Trailing commas are fine, as PHP allows them.
- Lists vs maps. A bracket whose first item is a
'key' =>pair becomes a JSON object; otherwise it becomes a JSON array. Explicitly numeric-keyed arrays such as[0 => 'a']are not supported and report an error. - Single quotes only inside strings. Quotes are swapped for double quotes textually, so a string that itself contains an apostrophe (
'it\'s') can make the conversion fail. Use double-quoted PHP strings or remove the apostrophe. - Only literals are supported. Function calls, constants, variables and
::classexpressions cannot be evaluated; replace them with literal values first.
FAQ
What happens if the input is invalid? The output pane shows an Invalid PHP array message with the parser error so you can find the line.
Is it safe to paste real config? Yes. The code is parsed as text in your browser and never executed or uploaded.
Need the reverse? Use JSON to PHP Array.