Convert JSON to a PHP Array — and Back
Turn JSON into PHP array code, or a PHP array into JSON, with PHP's own quoting, escaping and key rules
Your data stays with you. Conversion happens inside the browser; nothing is sent to a server.
Did this tool do the job?
Thanks, your feedback came through.
How it works
Hard-coding an API response or a settings file as a PHP array means rewriting the JSON by hand: curly braces become square brackets, every colon becomes "=>", and every string has to follow PHP's quoting rules. Paste the JSON into the left panel and the right panel shows code that starts with "<?php" and can be saved and included as it is. In the sample, the "address" object becomes a nested keyed array and the "tags" list becomes an array without keys; the single quotes in "'hand made'" are escaped, and the note that contains a line break is written in double quotes so that "\n" can stand for the newline. Leave the variable name empty and the code becomes return [...]; which is the shape Laravel and Symfony config files expect. For the other direction, paste a PHP array (with or without $data = or return in front): the output matches what PHP's json_encode prints with JSON_PRETTY_PRINT, except that accented letters stay readable instead of turning into \u00e9 escapes.
This tool is also known as json to php array, json to php array converter, php array to json, php array to json converter, convert json to php array online, json_decode to array.
What is PHP array?
A PHP array is the language's one general-purpose data structure: an ordered set of key-value pairs. The same structure holds a plain list such as 'apple', 'pear' and a record with named fields such as 'city' => 'Boston'. Keys are integers or strings; values can be strings, numbers, true/false, null or other arrays. PHP has no separate list and object types like JSON does, so the order of the keys decides which one you get when converting.
What is json_encode and json_decode?
json_encode turns a PHP value into JSON text and json_decode turns JSON text back into a PHP value; both are built into PHP. By default json_decode returns JSON objects as stdClass objects; pass true as the second argument and you get nested arrays like the ones this page writes. By default json_encode also escapes non-ASCII letters as \u sequences and slashes as \/; the JSON_UNESCAPED_UNICODE and JSON_UNESCAPED_SLASHES flags switch that off.
What is the difference between JSON and a PHP array?
JSON is a text format defined for moving data between languages: keys are always double-quoted strings, and trailing commas, comments and single quotes are not allowed. A PHP array is part of PHP source code: single-quoted strings, integer keys, trailing commas and comments are all fine, but only PHP can read it. Use JSON when the data goes to another program, a browser or an API; use a PHP array when it lives inside your PHP project as fixed configuration or test data.
What changes when a PHP array becomes JSON
PHP has a single array type that serves both as an ordered list and as a key-value map. JSON keeps those apart: square-bracket arrays and curly-brace objects. This page applies the same rule as json_encode: if the keys run 0, 1, 2 … in order with no gaps, the result is a JSON array; if even one key differs, it is a JSON object. So [3 => 'x', 1 => 'y'] turns into {"3": "x", "1": "y"}, and an empty array is always [], never {}.
Keys are read with PHP's own casting rules as well. An integer-looking string key such as '7' becomes the integer 7, while '07' stays a string; a true key becomes 1 and a null key becomes an empty string. If the same key appears twice, the last value wins and keeps the first position, exactly as in PHP, and the status line tells you how many keys were repeated. An element written without a key gets the largest integer key so far plus one.
- ['a', 'b'] → ["a", "b"] (a sequential list stays a JSON array)
- [1 => 'a', 2 => 'b'] → {"1": "a", "2": "b"} (does not start at 0)
- ['7' => 'x'] → the key becomes the integer 7; '07' stays a string
- 1.0 → 1.0 (float kept), 0x1A → 26, 1_000 → 1000
What cannot be converted: constants, function calls, interpolated strings
This tool does not run PHP code; it reads literal values only: single- and double-quoted strings, integers, floats, true, false, null and nested arrays. Constants such as PHP_EOL or __DIR__, calls such as date(), concatenation like 'a' . 'b', and double-quoted strings with a variable inside ("Hello $name") only get their value when the program runs. Instead of guessing, the tool stops and reports the line and column. For such an array, write the value in by hand or run echo json_encode($array); in PHP.
In the other direction, watch out for large integers. A JSON number like 12345678901234567890 is above PHP's 64-bit integer limit (9223372036854775807); when PHP reads that line it turns the number into a float and the last digits get rounded. Keep long numbers you never calculate with — IDs, card or barcode numbers — as strings (in quotes) in the JSON to avoid that loss.
Frequently asked questions
How do I convert JSON to a PHP array?
Paste the JSON into the left panel of this page and the right panel shows PHP code in the form $data = [...];. To convert at runtime inside your own code, use json_decode($json, true) in PHP; without true as the second argument the result is a stdClass object, not an array.
How do I convert a PHP array to JSON?
Paste the array, including the $data = [...]; or return [...]; line, into the left panel; the direction switches to PHP → JSON on its own. The result is the same as json_encode($data, JSON_PRETTY_PRINT), except that non-ASCII letters stay readable instead of becoming \u escapes (what the JSON_UNESCAPED_UNICODE flag does in PHP).
Is there a difference between [ ] and array( )?
No, both create the same array. The short square-bracket syntax has existed since PHP 5.4 (2012) and is what current coding standards use. Pick array( ) in the settings only if the code has to run on PHP 5.3 or older.
Why are some strings in single quotes and others in double quotes?
In PHP a single-quoted string leaves \n and $ untouched, so single quotes are the default and only the backslash and the single quote are escaped. When a string contains an invisible character such as a line break or a tab, double quotes are used, because \n only means a newline inside double quotes; the $ sign is then written as \$ so PHP does not treat it as a variable.
Is the data I paste sent to a server?
No. Reading the JSON or PHP, converting it and preparing the download all happen in your browser; even a config array with database passwords or API keys never leaves this page.