PHP array to JSON converter

Reads a PHP array literal as written in code and gives you the JSON that json_encode would produce, without running any PHP.

Loading the tool…

Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.

How to use it

  1. Enter php array and indent.
  2. The result appears straight away and updates as you edit.
  3. Copy the result, or copy a link that keeps the same values to share it.

How it is calculated

String keys holding canonical integers such as '5' become integer keys, and an item without a key takes the largest integer key plus 1 (PHP 8.3 rule). If the keys are exactly 0, 1, 2 in order the array becomes a JSON list, otherwise a JSON object. Hex, binary and octal numbers become decimal.

Example

Inputs: Indent = 2 spaces. Result: JSON: { "name": "ToolSahla", "version": 2, "debug": false, "langs": [ "ar", "en" ], "db": { "host": "localhost", "port": 3306 } }; Values converted: 7.

$config = [
    'name' => 'ToolSahla',
    'version' => 2,
    'debug' => false,
    'langs' => ['ar', 'en'],
    'db' => array('host' => 'localhost', 'port' => 3306),
];
{
  "name": "ToolSahla",
  "version": 2,
  "debug": false,
  "langs": [
    "ar",
    "en"
  ],
  "db": {
    "host": "localhost",
    "port": 3306
  }
}

Limits and caveats

  • Only literal values are read: variables, constants and function calls give an error.
  • Heredoc, nowdoc and float keys are not supported.

Questions about this tool

Can I paste $config = [ ]; as a whole?

Yes. The tool skips <?php, return, $name = and the final semicolon, and accepts //, # and /* */ comments.

Why did my array become a JSON object?

Because its keys are not exactly 0, 1, 2 in order. json_encode does the same, so [3 => 'a'] becomes {"3": "a"}.

How are duplicate keys handled?

The last value wins and the key keeps its first position, exactly as in PHP.