PHP arrays often appear in configuration files, debug output, and copied var_export() results, while APIs and browser tools usually expect JSON. This converter reads a supported PHP literal and produces indented JSON without executing the source as PHP.
Use it when you need to move representative data between the two formats or inspect a nested PHP array more easily. Conversion happens locally in your browser, and the output updates shortly after you edit the input.
How to Use the PHP to JSON Converter
Paste a PHP literal, review the generated JSON, and adjust its presentation with the visible controls.
- Keep the PHP to JSON tab selected under Direction.
- Paste an array literal, a
returnstatement, an assignment, or supportedvar_export()output into PHP array. - Choose 4 spaces, 2 spaces, or Tab from Indent.
- Read the converted value in JSON; if conversion fails, check the status message for the reported line.
- Select Copy to copy the JSON output, or select Swap to move it into the input and switch to JSON to PHP.
Which PHP Syntax Can This Converter Read?
The parser accepts short arrays such as ['name' => 'Mina'] and long-form arrays such as array('name' => 'Mina'). It also reads nested arrays, integers, decimal and exponent numbers, hexadecimal, binary and octal integer literals, booleans, null, single-quoted strings, and double-quoted strings without variable interpolation. Line comments beginning with // or # and block comments are ignored.
An optional opening <?php tag is accepted. The value may follow return, or it may be assigned to a simple variable before the array. A trailing semicolon and closing ?> tag are also accepted, so a common configuration fragment can be pasted directly.
<?php
return [
'service' => 'billing',
'retry' => 3,
'enabled' => true,
];
This is a literal parser, not a PHP runtime. It does not evaluate variables, constants, function calls, concatenation, arithmetic, environment lookups, or interpolated variables inside double-quoted strings. Convert those expressions to their resulting literal values first.
How PHP Array Keys Become JSON Arrays or Objects
PHP uses one array type for both ordered lists and key-value maps. JSON separates those concepts into arrays and objects. The converter emits a JSON array only when the PHP keys form a sequential zero-based list in encounter order; otherwise it emits a JSON object.
Explicit integer keys affect the next implicit numeric key in the same way the parser models PHP array construction. Boolean keys become 0 or 1, null becomes an empty-string key, numeric keys are truncated to integers, and integer-like strings are converted to numeric keys. When the same key appears again, the later value replaces the earlier one.
JSON object property names are strings, so a sparse PHP array such as [2 => 'x'] becomes an object with a property named "2". That output preserves the mapping more faithfully than filling missing list positions. If the distinction matters to an API contract, inspect the result rather than assuming every PHP array becomes a JSON array.
For work in the other direction, switch to JSON to PHP or open the JSON to PHP converter. To validate or compact the result separately, use the JSON formatter.
Strings, Numbers, and Values Without a JSON Equivalent
Single-quoted strings decode escaped backslashes and single quotes. Double-quoted strings recognize common PHP-style escapes, including newline, tab, hexadecimal, octal, and Unicode code-point escapes. They are not evaluated for $variable interpolation; the converter reports that case instead of guessing a value.
PHP numeric literals are converted to JavaScript numbers before JSON is written. Very large integers can therefore lose precision beyond JavaScript's safe integer range. If an identifier must retain every digit, quote it as a PHP string before conversion and verify that it remains a JSON string.
INF, -INF, and NAN have no JSON number representation, so the parser rejects them. Objects written in the \Class::__set_state(array(...)) shape produced by var_export() are reduced to their contained properties; the PHP class identity and restoration behavior are not preserved. A cast such as (object)[...] similarly produces a plain JSON object.
Common Conversion Errors and Privacy Considerations
An error usually points to the line where parsing stopped. Check for an unsupported expression, a missing comma, an unmatched bracket or parenthesis, or text remaining after the value. A PHP file containing application logic cannot be converted merely because it eventually returns an array.
The converter processes its input in the browser and does not upload it for conversion. That reduces exposure, but configuration arrays may still contain database passwords, API tokens, private hostnames, or personal data. Prefer sanitized samples, and remove secrets before copying output into tickets, chat messages, or source control.
Treat the JSON as converted data, not as proof that the source is safe or semantically correct. The tool does not run PHP, resolve references, validate an application schema, or determine whether the resulting settings are appropriate. If you are preparing generated types from sanitized output, the JSON to TypeScript converter can help describe its shape.
Frequently Asked Questions
Can this converter process an entire PHP file?
It can process a file-shaped fragment containing an optional <?php tag followed by a literal value, return value, or simple variable assignment. It does not execute general PHP code, functions, constants, includes, conditionals, or computed expressions. Reduce the source to a supported literal before converting it.
Does it support both PHP array syntaxes?
Yes. It accepts short bracket syntax, such as ['a' => 1], and long array('a' => 1) syntax. The two forms are converted according to their keys and values, so either may produce a JSON array or a JSON object.
Why did my PHP array become a JSON object?
JSON arrays represent sequential positions, while a PHP array may contain string keys, sparse numeric keys, or keys that start somewhere other than zero. The converter uses a JSON array only for a sequential zero-based list. Other key arrangements become object properties so missing positions are not invented.
Does the converter preserve PHP objects and class names?
No. Supported Class::__set_state(array(...)) output is converted to the contained data as a plain object, without retaining the class name or calling __set_state(). An (object) cast is also represented as ordinary JSON object data. This is a data conversion, not object reconstruction.
Is sensitive PHP configuration uploaded?
No conversion request is sent by this tool; parsing and JSON generation run in your browser. Still, production configuration can contain valuable secrets, and copied output can leak through other channels. Redact credentials and personal data before using or sharing the result.
References: PHP manual: Arrays · PHP manual: var_export.