ToolConvoyToolConvoyv2.6
DEV

JSON to PHP Array — Convert JSON to PHP Array Syntax

Convert JSON to PHP array syntax with proper indentation. Associative vs indexed, PSR-12 formatting — in your browser, no upload.

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

JSON to PHP Array

Convert JSON to PHP array syntax. Generates clean PHP array code with proper indentation. Works entirely in your browser.

PHP’s array literal syntax is one of the most copied syntaxes in web development. JSON is essentially a constrained subset of JavaScript’s object literal syntax, and PHP array syntax is its closest cousin in the server-side world. The conversion from JSON to a PHP array is less about data transformation and more about syntax translation: the structure stays the same, the types map cleanly (JSON string ↔ PHP string, JSON number ↔ PHP int or float, JSON boolean ↔ PHP bool, JSON null ↔ PHP null), and the only real decision is whether to use short array syntax ([...]) or the legacy array(...) form.

The assymetry that trips up most converters is PHP’s associative-indexed distinction. PHP has a single array type that serves both as a list (indexed, numeric keys) and as a dictionary (associative, string keys). JSON has two distinct types: arrays (ordered, numeric indices) and objects (unordered, string keys). A converter must decide: does a JSON object with numeric keys become an associative array (because the keys are technically strings) or an indexed array (because the keys are sequential numbers starting at 0)? The generator covers both cases by emitting a comment at the top of the output explaining the decision for any ambiguous key patterns.

The most common downstream step after generating a PHP array is to drop it into a Laravel or Symfony config file, a test fixture for a PHPUnit test, or a data provider method that returns an array of test cases. If the destination is a WordPress theme or plugin, the generated array works as an options array for register_post_type, wp_register_script, or any of the dozens of WordPress registration functions that accept associative arrays. The PHP array is the universal config language of the PHP ecosystem — the generated output is paste-compatible with all of them.

Advertisement

How to use

  1. Paste your JSON

    Drop a JSON object or paste a JSON string. The converter auto-detects the nesting depth and emits an indented PHP array with the same structure.

  2. Choose array style

    Toggle between short array syntax (`[...]`), which is PSR-12 standard, and the older `array(...)` form for legacy codebases. Short syntax is the default.

  3. Copy or download the .php snippet

    Copy the PHP array to your clipboard or download as a .php file. The output is paste-compatible with any PHP config, router, or test fixture — no extra functions needed.

Frequently asked

How does it determine associative vs indexed arrays?

JSON objects become PHP associative arrays with string keys. JSON arrays with numeric keys (0, 1, 2, ...) become indexed arrays with implicit numeric keys. A JSON object where every key is a number becomes an associative array because the keys are strings — convert to indexed by hand if needed.

Does it preserve JSON null values?

Yes. JSON `null` becomes PHP `null`. Empty JSON objects become `[]`. Empty JSON strings become `''`. The round-trip through `json_encode` is lossless for the standard JSON types.

Will it handle a deeply nested JSON file?

Yes. The indentation increases by 4 spaces per level (PSR-12 standard) and there is no depth limit beyond the browser's memory. Deeply nested JSON produces deeply indented PHP — that's expected.

Can I use the output directly in a Laravel config file?

Yes. Laravel config files return a PHP array. Paste the generated array as the return value of a config file and add `<?php return ...;` at the top. The indentation matches Laravel's own config style.

How does it handle JSON with duplicate keys?

JSON technically forbids duplicate keys, but some APIs emit them. The converter keeps the last value for each key — the same behaviour as PHP's `json_decode` in associative mode. Review the output if duplicate keys are suspected.

Limitations

  • No TrailingComma optionThe output uses trailing commas after every element (PHP 7.3+ style). For PHP 7.2 and earlier, remove trailing commas by hand.
  • String key quoting is always single-quoteString keys are emitted as single-quoted PHP strings. If a key contains a single quote, it is not escaped. PSR-12 encourages single-quoted keys for non-interpolated strings; multi-byte characters in keys are rendered as-is.
  • No string interpolationThe output is a static array literal. If you need a value computed at runtime, replace the literal value with a function call or variable reference by hand after generation.

Platform notes

macOS
PHPStorm, VS Code with Intelephense, and Sublime Text all accept the generated array. Use the browser tool for one-off conversion of a sample API response to a test fixture.
Windows
XAMPP, WAMP, and Laragon setups handle the generated array the same way. The browser tool is the right pick for converting a JSON snippet from a chat or email to a PHP config array.
Linux
For command-line work, `php -r 'var_export(json_decode(file_get_contents("f.json"), true));'` is the CLI equivalent. The browser tool is the right pick for one-off conversion where a PHP runtime is not available.
Web
Runs entirely client-side. Works offline. The conversion takes milliseconds.
Advertisement
Advertisement