Xerobit

PHP Array to JSON Converter

Use this php array to json converter to transform PHP array literals into valid JSON instantly. The tool handles nested arrays, both quote styles, unquoted keys, and trailing commas. It runs entirely in the browser so nothing is uploaded to any server.

Paste a PHP array() or [...] literal here.

Format the JSON output with indentation.

JSON Output

{
  "name": "Alice",
  "age": 30
}

By · Published · How we build and test tools

How to use the php array to json converter

  1. Paste your PHP array literal into the text area. Both array() and short [] syntax are supported.
  2. Toggle Pretty print if you want indented JSON output, or leave it off for compact output.
  3. The converted JSON appears immediately below as you type.

How the php array to json conversion works

A PHP array uses either array(key => value) or [key => value] syntax. To produce valid JSON, the converter applies several transformations to map the php array to json correctly:

  • The outer array(...) wrapper is replaced with {...} for associative arrays or [...] for indexed lists.
  • Single-quoted strings become double-quoted strings, because JSON requires double quotes around all string values and object keys.
  • The => operator becomes a colon :.
  • Unquoted keys (bare words) are wrapped in double quotes.
  • PHP literals true, false, and null map directly to their JSON equivalents.
  • Trailing commas before a closing bracket are removed, since JSON does not allow them.

For example, the input ['status' => true, 'count' => 42,] becomes {"status": true, "count": 42}. This recursive parsing ensures that deeply nested structures are handled without errors.

Supported syntax reference

The converter handles the most common PHP array constructs. Here is a quick reference of what is supported when you convert a php array to json:

PHP InputJSON Output
['a' => 1]{"a": 1}
array("b" => 2){"b": 2}
[1, 2, 3][1, 2, 3]
['k' => true]{"k": true}
['k' => null]{"k": null}
[key => 'val']{"key": "val"}
['a' => 1,]{"a": 1}

Nested structures are fully supported. A deeply nested array() inside a short [] array will be parsed recursively, preserving the exact hierarchy in the resulting JSON.

Common mistakes when converting manually

When developers convert a PHP array to JSON by hand, a few errors come up repeatedly. Forgetting to change single quotes to double quotes is the most common issue, since JSON strictly requires double quotes for strings and keys. Another frequent mistake is leaving trailing commas at the end of an array or object; PHP allows them but JSON parsers reject them outright.

Unquoted keys also cause problems. Constants used as values (such as PHP_EOL or a class constant) cannot be resolved and produce an error, and you may paste a whole <?php return [...]; or $x = array(...); statement. In PHP you can sometimes use bare constants as keys, but JSON demands that every object key be a double-quoted string. Finally, mixing up numeric indices matters: a PHP array with non-sequential integer keys like [0 => 'a', 5 => 'b'] should become a JSON object rather than a JSON array, because JSON arrays must be contiguous starting from zero. Using a dedicated php array to json tool avoids these pitfalls entirely.

Why convert PHP arrays to JSON

JSON is the standard data interchange format for web APIs, configuration files, and frontend applications. While PHP arrays are convenient within a PHP codebase, they cannot be read natively by JavaScript, Python, or other languages. Converting your data structure from a php array to json makes it portable across different systems and programming environments.

Developers frequently need to extract hardcoded configuration arrays from legacy PHP scripts and move them into modern JSON-based config files. Instead of rewriting the data by hand and risking typos, a reliable converter parses the original syntax exactly and outputs standards-compliant JSON. This is especially useful when migrating older projects to newer frameworks that expect JSON configuration.

Worked examples

ExampleInputResult
Simple associative arrayPHP Array: ['name' => 'Alice', 'age' => 30]; Pretty print: yes{ / "name": "Alice", / "age": 30 / }
Nested array()PHP Array: array('user' => array('id' => 1, 'active' => true)); Pretty print: yes{ / "user": { / "id": 1, / "active": true / } / }
Indexed listPHP Array: [1, 2, 3, 'four']; Pretty print: no[1,2,3,"four"]

FAQ

Does this tool support nested PHP arrays?

Yes. The parser handles arbitrary nesting of both array() and short [] syntax, converting each level into the corresponding JSON object or array.

Can I convert a PHP array with single quotes?

Yes. Single-quoted strings are automatically converted to double-quoted strings, which is required by the JSON specification.

What happens to trailing commas?

Trailing commas are valid in PHP but invalid in JSON. The converter removes them automatically so the output parses correctly.

Are PHP comments preserved in the JSON output?

No. Standard JSON does not support comments. Any PHP-style comments in the input are stripped during conversion.

Is my PHP code sent to a server?

No. The conversion happens entirely in your browser using JavaScript. Nothing is uploaded anywhere.

Does it handle boolean and null values?

Yes. PHP's true, false, and null are mapped directly to their JSON equivalents.