Overview
Introduction
PHP array literals show up in config files, fixtures, and legacy code that a JavaScript-based workflow still needs to read.
This tool parses that PHP array syntax directly into JSON, without needing a PHP runtime to eval it.
What Is PHP Array to JSON Converter?
A hand-rolled recursive-descent parser for PHP array literals: it tokenizes the source position by position, recognizing array(/[ to open an array, 'key' => value or bare value entries, and PHP's scalar literal forms.
The parser is deliberately narrow, PHP array literals only, not arbitrary PHP expressions like function calls, variables, or string interpolation inside array values.
How PHP Array to JSON Converter Works
After stripping an optional <?php tag and $var = ... ; wrapper, the parser walks the text character by character: array(/[ opens a new array body, each entry is parsed as a value, optionally followed by => and a second value if it's a keyed entry, and entries are separated by commas (a trailing comma before the closing bracket is fine).
Once every entry is collected, the tool resolves each entry's effective key, explicit keys keep their written value (stringified), positional entries get the next unused sequential index, then checks whether the full key set is exactly "0", "1", "2", ... in order to decide between emitting a JSON array or a JSON object.
When To Use PHP Array to JSON Converter
Use it to pull structured data out of a legacy PHP config file (like a WordPress or Laravel config array) as JSON.
It's also useful for checking, at a glance, what an array literal in a PHP code review will actually produce.
Often used alongside JSON to PHP Array Converter, JSON to TypeScript Converter and JSON Formatter & Beautifier.
Features
Advantages
- Understands both array(...) and short [...] syntax, plus trailing commas, without requiring PHP to be installed.
- Reports a specific line and column for malformed array syntax instead of failing silently.
- Directly accepts this site's own JSON to PHP Array output, including its <?php $data = ...; wrapper, with no manual editing.
Limitations
- Only parses array literals: PHP expressions inside a value position (variables, function calls, string interpolation, constants) aren't supported and will fail to parse.
- Empty array() / [] literals always become a JSON array ([]), since PHP itself doesn't distinguish an empty list from an empty map.
Examples
Best Practices & Notes
Best Practices
- Strip out any PHP code around the array literal (function calls, variable references) before pasting, since only literal array syntax is understood.
- Check the reported line/column against your source when parsing fails, most failures come from a missing => value or an unterminated string.
- Pair with JSON to PHP Array to round-trip and confirm a conversion looks the way you expect.
Developer Notes
The parser follows the same hand-rolled recursive-descent style as this site's Bencode and TOML decoders: a single position pointer walked forward by small parseValue/parseArrayBody functions that throw on failure, with the position captured at the throw site and translated to a line/column afterward, rather than building a separate tokenizer pass first.
PHP Array to JSON Converter Use Cases
- Extracting structured data from a legacy PHP config array as JSON
- Reviewing what a PHP array literal in a code diff actually evaluates to
- Round-tripping data generated by JSON to PHP Array back to JSON to verify the conversion
Common Mistakes
- Pasting a full PHP function or class instead of just the array literal; only the array(...) / [...] expression itself is parsed.
- Expecting PHP constants or variables used as array values to resolve; they aren't evaluated and will cause a parse error.
- Assuming an explicit '0' => key and a positional entry always combine predictably; only the resulting key set as a whole determines array vs. object output.
Tips
- If parsing fails, check for a stray variable, function call, or comment inside the array, those aren't valid array-literal syntax.
- Use JSON to PHP Array first if you want a guaranteed-parseable starting point for testing this converter.