JSON to PHP array: generate code from data
Sometimes you want data in PHP source rather than in a JSON file: a config array, a seeder, a fixture for a PHPUnit test, or a lookup table. This JSON to PHP array converter writes that code for you, with correct quoting and nesting, so you can paste it straight into a .php file.
Example
{
"name": "O'Brien",
"price": 9.5,
"active": true,
"manager": null,
"tags": ["php", "json"],
"meta": {},
"200": "OK"
}
With the default options:
<?php
$data = [
200 => 'OK',
'name' => 'O\'Brien',
'price' => 9.5,
'active' => true,
'manager' => null,
'tags' => [
'php',
'json',
],
'meta' => [],
];
How JSON maps to PHP
| JSON | PHP output |
|---|---|
| Object | Associative array with 'key' => value pairs |
| Array | List without explicit keys |
| String | Single-quoted string; ' and \ are escaped |
| Number | Integer or float literal |
true, false, null | true, false, null |
Key like "200" | Unquoted integer key 200 |
Single-quoted strings were chosen on purpose: PHP does not interpolate $variables or interpret \n inside them, so text such as "Price: $5" is reproduced literally. Every element, including the last, is followed by a comma, which PHP allows and which keeps diffs small.
Options
- Syntax – Short [ ] is the modern style available since PHP 5.4. array( ) produces the classic long form for older code bases or style guides that require it.
- Variable name – defaults to
data. Characters other than letters, digits and underscores are removed, somy-configbecomes$myconfig. - Include <?php tag – on by default. Turn it off when you paste into an existing file.
- Indent – 4 spaces by default, or 2, 3 spaces or a tab.
Details to check before you paste
- Integer keys move to the front. In the example,
200 => 'OK'appears first although it was last in the JSON. The conversion runs in JavaScript, which lists integer-like keys before other keys. Keys such as"01"are not integers and stay quoted in place. - Empty objects become empty arrays. PHP has one array type, so
{}and[]both become[]. If you later calljson_encodeon the array, it produces[], not{}. - Whole-number floats lose the decimal.
10.0is written as10, which PHP reads as an integer. - Line breaks in values are written as real line breaks inside the quoted string. That is valid PHP but spreads the string over several lines.
Using the generated array
A common pattern is a config file that returns the array directly. Keep the <?php tag, replace $data = with return, and the file can be loaded with $config = require 'config.php';. For test fixtures, keep the variable and include the file in your test case, or paste the array into a data provider method.
Static array or json_decode?
If the JSON arrives at runtime, from an API or a file that changes, keep it as JSON and call json_decode($json, true), which returns the same associative array structure. Generate static code only for data that belongs in the source. If you want typed PHP classes instead of arrays, use JSON to PHP classes. To tidy existing PHP, try the PHP formatter, and for XML input there is XML to PHP.
The conversion runs in your browser; your JSON is not uploaded unless you use Load URL or Save & Share.