1. Home
  2. JSON to PHP Array

JSON to PHP Array Converter

Paste JSON to get PHP array code you can drop into a config file or test. Switch direction to turn a PHP array into JSON with the real json_encode().

PHP array

JSON → PHP converts instantly with JavaScript. PHP → JSON runs PHP 8.4 in your browser. Nothing is uploaded.

How JSON maps to PHP

JSONPHP (json_decode with true)
{"a": 1}['a' => 1] associative array
[1, 2][1, 2] list with keys 0 and 1
"text"'text' string
42 / 4.2int / float
true, falsetrue, false
nullnull

Two details cause most surprises. First, numeric string keys become integers in PHP: the JSON key "10" turns into the integer key 10. Second, very large integers beyond PHP_INT_MAX become floats and lose precision; pass JSON_BIGINT_AS_STRING to keep them as strings.

When to paste JSON into PHP code

Converting JSON into PHP array syntax is useful for configuration files that return an array (Laravel and Symfony config, WordPress theme.json fallbacks), for test fixtures and data providers in PHPUnit, for seeders, and for small lookup tables that would otherwise need a file read on every request. Arrays in PHP files are also cached by OPcache, so they cost nothing to load.

For data that changes at runtime or comes from an API, keep it as JSON and decode it with json_decode(). Generating PHP code from external data and including it is a security risk.

PHP to JSON with the real json_encode()

The reverse direction evaluates your array with PHP itself, so it understands everything a PHP file can contain: array() and [], nested arrays, single and double quotes, escape sequences, constants like PHP_INT_MAX, trailing commas and expressions such as 60 * 60. Paste either a bare array, $data = [...]; or a whole config file that starts with <?php return [...];. The output uses JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES and JSON_UNESCAPED_UNICODE.

Working with serialized data instead of JSON? Use the PHP Unserialize tool. To experiment with json_encode() flags yourself, open the PHP Sandbox.

More PHP tools

Frequently asked questions

How do I convert JSON to a PHP array in code?

Use json_decode($json, true). The second argument true returns associative arrays; without it you get stdClass objects. Add JSON_THROW_ON_ERROR as the fourth argument so invalid JSON throws an exception instead of silently returning null: json_decode($json, true, 512, JSON_THROW_ON_ERROR).

How do I convert a PHP array to JSON?

Use json_encode($array). Add JSON_PRETTY_PRINT for indentation, JSON_UNESCAPED_SLASHES to keep / as is, and JSON_UNESCAPED_UNICODE to keep characters like “é” instead of \u00e9. A PHP list with keys 0, 1, 2… becomes a JSON array; any other keys produce a JSON object.

Why is an empty PHP array encoded as [] instead of {}?

PHP cannot tell whether an empty array was meant as a list or a map, so json_encode([]) gives []. Use new stdClass() or the flag JSON_FORCE_OBJECT when an API expects an empty object.

Does the PHP to JSON direction run my code?

Yes, in your browser only. The array you paste is evaluated by a real PHP 8.4 interpreter compiled to WebAssembly and passed to json_encode(), so constants, nested arrays and expressions behave exactly as in PHP. Nothing is sent to a server, and the sandbox has no network or file access.

Should I use short array syntax?

Yes, unless the code must run on PHP 5.3 or older. The short syntax [] has been available since PHP 5.4 and is required by the PER Coding Style. Switch the option to array() only for legacy code bases.