ToolConvoyToolConvoyv2.6
DEV

PHP Array to JSON — Convert PHP Array Syntax to JSON

Convert PHP array syntax to JSON. Handles short and long array syntax, associative keys, and trailing commas — in your browser, no upload.

● LOCAL · GENERATED IN YOUR TAB0 network requests from tools since page load

PHP Array to JSON

Convert PHP array syntax to JSON. Handles nested arrays and associative arrays. Works entirely in your browser.

PHP array syntax and JSON look similar enough that people sometimes treat them as interchangeable, and different enough that a naive copy-paste breaks. PHP uses => for key-value pairs where JSON uses :. PHP allows unquoted string keys where JSON requires double-quoted strings. PHP uses array() or [...] where JSON uses {} for objects and [...] only for arrays. The differences are cosmetic — the semantics are identical — but the syntax gap is wide enough that a manual conversion of a deeply nested PHP config array is a tedious, error-prone chore.

The parser handles the five most common PHP-to-JSON conversion pitfalls automatically. String keys: [name => 'Alice'] becomes {"name": "Alice"}. Constants: [flag => true] where flag is unquoted becomes {"flag": true} because recognised constant names map. Trailing commas: [1, 2, 3,] drops the final comma. Short array syntax: [...] is the default and produces a JSON object or array depending on the keys. The conversion is one-directional but the output is always valid JSON.

The real-world need for this tool is the Laravel-to-frontend data handoff. A Laravel developer writes a config array in PHP syntax, and the frontend developer needs the same data as a JSON object for a JavaScript or TypeScript component. The round-trip through JSON is cleaner than embedding the PHP array in a Blade template with @json() because the JSON output is a static artifact that can be committed to the frontend repo, versioned independently, and type-checked by the TypeScript compiler.

Advertisement

How to use

  1. Paste a PHP array

    Paste a PHP array literal using either short syntax `[...]` or legacy `array(...)`. The parser handles associative keys, nested arrays, string quoting, and integer values as they appear in real PHP code.

  2. Review the JSON output

    The output is standard JSON with pretty-printing. Null, boolean, and numeric types are inferred: PHP `null` becomes JSON null, PHP `true`/`false` become JSON booleans, and bare numeric literals become JSON numbers.

  3. Copy or download the JSON

    Copy the JSON to your clipboard or download as a .json file. The output is valid JSON per RFC 8259 and parseable by any standard JSON parser.

Frequently asked

How does it handle PHP constants and expressions?

PHP constants (like `PHP_EOL`, `true`, `false`, `null`) are resolved: `true` → `true`, `false` → `false`, `null` → `null`. Unrecognised constants and function calls are wrapped in strings as their literal source text. Arithmetic expressions are not evaluated — `1 + 2` stays as the string `'1 + 2'`.

What about heredoc and nowdoc string syntax?

Heredoc and nowdoc strings are unwrapped and the inner text is treated as a JSON string. Leading indentation in heredocs is stripped per the PHP 7.3 flexible heredoc rules. Empty heredocs become empty JSON strings.

Can it handle a PHP file with multiple array definitions?

The parser extracts the first top-level array literal it encounters. If the PHP file contains multiple arrays (e.g., a config file with several `return [...]` statements), only the first is converted. Paste each array individually for full coverage.

How are PHP integer keys handled in the JSON output?

PHP arrays with sequential integer keys (0, 1, 2, ...) become JSON arrays. PHP arrays with sparse or string keys become JSON objects. If a PHP array mixes numeric and string keys, the output is a JSON object with the numeric keys as string property names.

Does it preserve trailing commas in the source?

Trailing commas in PHP arrays are silently ignored during parsing — they are a syntax convenience in PHP and have no meaning in JSON. The output JSON does not include trailing commas, as JSON syntax does not permit them.

Limitations

  • No expression evaluationFunction calls, variable references, and inline calculations are not evaluated. `['date' => date('Y-m-d')]` becomes `{"date": "date('Y-m-d')"}` — a string, not the evaluated result.
  • Unquoted string keys may parse unexpectedlyPHP allows unquoted string keys like `[key => value]`. The parser treats these as string keys. If the unquoted key collides with a recognised constant name, the constant value is used instead — `[true => 1]` becomes `{"1": 1}`.
  • Single line per entry in the source assumedThe parser works best with well-formatted PHP arrays. Minified or single-line PHP arrays parse correctly but produce less helpful error messages on malformed input.

Platform notes

macOS
PHPStorm and VS Code both format PHP arrays well. The browser tool is the right pick for converting a PHP array snippet from a chat or email to JSON for use in a non-PHP tool.
Linux
For command-line conversion, `php -r 'echo json_encode([...]);'` does the same. The browser tool is the right pick when a PHP runtime is not available.
Web
Runs entirely client-side. Works offline once loaded.
Advertisement
Advertisement