JSON to PHP Array
Paste a JSON document and get a PHP array literal formatted for a .php source file, short array syntax ([...]), proper indentation, strings quoted correctly, nested objects becoming associative arrays and arrays staying indexed. Useful for embedding API fixtures in tests or seeding config files.
How to convert JSON to PHP
-
1
Paste the JSON
Any valid JSON: objects, arrays, deeply nested.
-
2
Pick syntax
Short `[...]` (PHP 5.4+) or long `array(...)` (rarely needed now).
-
3
Pick string quotes
Single-quoted (no interpolation, escape apostrophes) or double-quoted (allows `$` interpolation: usually not what you want for data).
-
4
Copy the array
Paste into your `.php` file, assign to a `return`, or wrap in `config([...])`.
Example
Input:
{ "app": { "name": "Hyperion", "debug": true, "hosts": ["h1.example.com", "h2.example.com"] } }
Output (short syntax, single quotes):
[
'app' => [
'name' => 'Hyperion',
'debug' => true,
'hosts' => [
'h1.example.com',
'h2.example.com',
],
],
]
Type mapping
| JSON | PHP |
|---|---|
| string | string (single-quoted) |
| integer | int |
| number (decimal) | float |
| boolean | true / false |
| null | null |
| array (JSON) | indexed array |
| object (JSON) | associative array |
When to use associative vs indexed
PHP has one array type with both behaviours. JSON clearly distinguishes object and array, so the mapping is:
{}-> associative (['key' => value]).[]-> indexed ([value, value]).
The generator always preserves the distinction; mixing the two in PHP is legal but confusing.
PHP-specific gotchas
- Numeric-string keys. PHP casts
"42"to the integer key42in an array. If your JSON uses numeric strings as keys and you need them as strings, thearray_combineround-trip will not preserve them. - Large integers. JSON can hold integers larger than
PHP_INT_MAXon 32-bit builds. The generator warns when that might happen. - UTF-8. PHP strings are byte sequences; the generator emits UTF-8 literally. Ensure your
.phpfile is saved as UTF-8 without BOM.
Use cases
- Config files: Laravel and Symfony both use PHP-array config. Dropping a JSON config vendor-style works well.
- Test fixtures:
PHPUnitfixtures are cleaner as arrays than as JSON files loaded at runtime. - Seeds: database seeders in Laravel often start as a JSON dump and become PHP arrays.
Common mistakes
- Round-tripping PHP arrays back to JSON.
json_encodetreats an associative array as an object and an indexed array as an array. If you mix them (['key' => 'val', 0 => 'x']), you get an object on the way out. Keep JSON-shaped arrays JSON-shaped. - Double quotes with apostrophes. Single-quoted PHP strings need escaped
\'; double-quoted strings need escaped\\"and variables interpolated accidentally if you are not careful. Stick to single quotes for data. - Forgetting trailing commas. PHP allows them since PHP 7.3 and they make diffs cleaner. The generator adds them by default.
Frequently Asked Questions
Short syntax ([...]) has been available since PHP 5.4 and is the idiomatic choice for all modern code. Long syntax (array(...)) is needed only for code that must run on PHP 5.3 or earlier, essentially legacy systems.
Yes. Laravel config files are plain PHP arrays returned from a file. Paste the generator output after return and save it in config/.
Single-quoted PHP strings do not interpolate variables or escape sequences (except \' and \\). For data literals, this is faster and safer. Double quotes only matter when you want \n, \t, or $variable inside the string.
No, it emits an array literal. Wrap it yourself with return, const, or a class property as needed. Keeping the output small keeps it composable.
Related Tools
ASCII Table Reference
Full ASCII table from 0 to 127 with decimal, hex, octal, binary, standard names and HTML numeric-reference notation, including NUL, LF and DEL.
Color Palette Generator
Generate monochromatic, analogous, complementary, triadic or tetradic color palettes from a base HEX color and export copy-ready CSS variables.
HTML Character Reference
Searchable list of HTML entities, their named and numeric codes, and a one-click copy for special characters and symbols.
FPS Counter
Measure browser FPS with requestAnimationFrame, smoothing, min/max frame rate, warnings and an optional graph. Runs locally with no upload or API.
HEX Color Picker
Pick or enter a HEX colour and get RGB, HSL, approximate CMYK, relative luminance and contrast ratios against white and black.
JSON Formatter
Paste JSON to pretty-print with 2 or 4 spaces, minify it to compact output, or run a quick syntax check before copying the result.
Tool available in other languages
- JSON vers tableau PHP [FR]
- JSON을 PHP 배열로 변환 [KO]
- تحويل JSON إلى مصفوفة PHP [AR]
- JSON sang mảng PHP [VI]
- JSON ke Array PHP [ID]
- JSON naar PHP-array [NL]
- JSON till PHP-array [SV]
- JSON do tablicy PHP [PL]
- JSON เป็น PHP Array [TH]
- JSONからPHP配列へ [JA]
- JSON zu PHP-Array [DE]
- JSON a Array de PHP [ES]
- JSON para Array PHP [PT]
- JSON in array PHP [IT]
- JSON в массив PHP [RU]
- JSON'dan PHP dizisine [TR]
- 从 JSON 到 PHP 数组 [ZH]