JSON to PHP Array
Convert JSON into a PHP array literal with correct single-quote escaping, short or long syntax and configurable indentation.
About this literal
Conversion happens entirely in your browser. Strings are single-quoted so dollar signs stay literal rather than being interpolated as variables.
What It Does
Turns a JSON document into a PHP array literal you can paste straight into a source file. It is for the case where the data genuinely belongs in your code — a config block, a lookup table, a test fixture, a seed file — rather than for anything arriving over the wire, which json_decode should handle at runtime. The output uses single-quoted strings throughout, which is deliberate: PHP interprets only two escape sequences inside them, so a value containing a dollar sign stays literal instead of being interpolated as a variable. JSON arrays become PHP lists with implicit integer keys and objects become associative arrays, which is exactly the mapping json_decode produces, so the literal matches what decoding would have given you.
When to Use It
- A configuration block arrived as JSON and belongs in a PHP config file rather than being read at runtime.
- You need a test fixture as a literal so the expected value is visible in the test itself.
- A seed file needs data you already have as JSON from an export or an API response.
- You are moving a lookup table out of a database and into code where it can be version controlled.
- A colleague sent a payload and you want it in a shape you can paste into a PHP script to experiment with.
Worked Examples
{"name":"Jane","age":30,"active":true,"nickname":null}
The basics. PHP spells true, false and null the same way JSON does, so those pass through unchanged — unlike Python, where getting this wrong produces code that fails immediately.
{"note":"O'Brien's $rate went up"}
The escaping case. The apostrophe is escaped with a backslash, and the dollar sign is left completely alone — single quotes do not interpolate, which is why they are the right choice for data.
{"users":[{"id":1},{"id":2}],"meta":{"total":2}}
Mixed structures. The users array becomes a PHP list with implicit numeric keys, while meta becomes an associative array with quoted string keys — the same mapping json_decode would produce.
Features
How to Use
1. Paste your JSON into the input box. 2. The PHP array appears immediately below. 3. Choose short or long array syntax to match the file you are pasting into. 4. Set the indent width and the variable name, or clear the name for a bare literal. 5. Copy the result into your PHP source.
Common Mistakes
- Using double-quoted strings for data. PHP interpolates them, so a value containing $name becomes whatever that variable holds — or an empty string and a warning.
- Pasting a runtime payload into source. Data arriving over the wire should be decoded with json_decode; embedding it freezes a snapshot that will drift out of date silently.
- Embedding a very large document as a literal. A few hundred lines is reviewable in a diff; ten thousand is not, and belongs in a data file.
- Assuming huge integers survive. Values beyond PHP_INT_MAX become floats and lose exactness — quote large ids as strings before converting.
- Mixing array syntaxes in one codebase. Both work identically, but consistency with the surrounding file matters more than which one you prefer.