Tools / JSON ↔ PHP Array Converter

JSON ↔ PHP Array Converter.

Turn JSON into a PHP array literal and read PHP arrays, including array() syntax, back into JSON.

Runs in your browser

short writes [ ], long writes array( ).

Ctrl/⌘ + Enter to run · Ctrl/⌘ + Shift + C to copy the result

Result

About JSON ↔ PHP Array Converter

The JSON ↔ PHP Array Converter writes JSON as a ready-to-paste PHP array and reads PHP array literals back into JSON. JSON objects become associative arrays with 'key' => value pairs, lists become arrays without keys, strings are single-quoted with backslashes and apostrophes escaped, and true, false and null keep their PHP spelling. You can choose the modern short [ ] syntax or the classic array( ) form, the indentation and whether to add trailing commas, which keeps diffs small in Laravel or Symfony config files. For example {"mail": {"port": 587}} becomes ['mail' => ['port' => 587]]. The reverse direction parses PHP source without executing it: both array syntaxes, nested arrays, single- and double-quoted strings with their escape sequences, nowdoc and heredoc, hexadecimal, octal and binary numbers, (object) casts, comments, an optional <?php tag and a leading return or $variable = assignment. Implicit keys follow PHP rules, so after 5 => 'five' the next value gets key 6, and the original key order is preserved in the JSON output. Anything that would need PHP to run, such as constants, function calls, variables or string interpolation, is reported with its line and column rather than guessed. A frequent surprise is that numeric string keys like "1" are cast to integers by PHP; the converter mirrors that. All processing happens locally in your browser.

How to Use

  1. Paste JSON or a PHP array and choose the direction.
  2. Pick the PHP syntax, indentation and trailing comma style.
  3. Copy the result into your config file, fixture or test.

Frequently Asked Questions

Does the PHP to JSON direction run my code?

No. It parses the array literal only. Constants, function calls, variables and interpolated strings are reported as errors because they would need PHP to evaluate.

When does a PHP array become a JSON array instead of an object?

When its keys are exactly 0, 1, 2 … in order, as PHP's json_encode does. Any other keys, or an (object) cast, produce a JSON object.

Why did my key "10" turn into a number?

PHP casts integer-like string keys to integers. The converter applies the same rule, so the PHP output matches what PHP would store.

Is the data sent to a server?

No. Conversion runs in your browser and nothing is uploaded.

Related Tools