Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Free tool · Developer tools

JSON to PHP array converter

This JSON to PHP array converter turns pasted JSON into PHP source you can drop into a file. The sample object with 12 keys becomes 16 lines of PHP, with false and null kept as PHP constants and the path C:\www\site written as 'C:\\www\\site'.

  • Free
  • No sign-up
  • Runs in your browser

By Maaz Zindani · Updated

Array syntax
String quotes
Layout
Wrap the array in
Options
Used with the variable wrapper. Write it without the dollar sign.
PHP array

array, 8 keys16 lines, 3 levels deep, 329 bytes

  • Short [ ] syntax
  • Single quotes where possible
  • 4 space indent
  • $config assignment
JSONPHPCount
objectarray with string keys3
arrayarray with keys 0, 1, 21
stringstring5
number without a fractionint3
number with a fraction or exponentfloat1
falsefalse1
nullnull1

How to use the JSON to PHP Array Converter

  1. Paste the JSON into Your JSON or load a file from your device. A mistake is reported with its line and column.
  2. Choose Array syntax (square brackets or array()), the String quotes and the Layout.
  3. Pick how the array is wrapped: a variable assignment, a return statement for a config file, or nothing at all.
  4. Press Copy or Download .php. Add the opening php tag when the result will be a whole file.

How do I convert JSON to a PHP array?

A JSON object becomes a PHP array with string keys written as key => value, and a JSON array becomes a PHP array with the keys 0, 1, 2 left implicit. The sample has an object inside an object, so the output nests the same way. Its first lines read: $config = [ 'app' => 'Invoice Desk', 'debug' => false, Numbers keep the exact text you typed, so 2.5 stays 2.5 and a long digit string is never rounded on the way. The last entry of the sample is an owner whose id is the string 12, and it stays a string in PHP because JSON wrote it in quotes.

The Layout choice decides how much room the array takes. One line gives ['a' => [1, 2], 'b' => ['c' => null]] for a small object, and the array() syntax writes the same data as array('a' => array(1, 2), 'b' => array('c' => null)). The two forms are equivalent in PHP, so the choice is a matter of house style. Trailing commas are on by default because PHP allows a comma after the last element and the manual recommends it for multi-line arrays, which keeps later edits to one line in a diff.

The wrapper turns the literal into a statement. A variable gives $config = [...]; and return gives a file that an include call hands back, the pattern many frameworks use for configuration. With the php tag switched on, a small object comes out as: 7, 'ok' => true ];

How are strings and special characters written in PHP?

PHP treats the two quote styles differently. In single quotes only the backslash and the single quote itself need an escape, and a sequence such as \n is printed as the two characters it shows. In double quotes PHP reads escapes such as \n and \t and also replaces a $ followed by a name with the value of that variable. For that reason the converter escapes every dollar sign in double quotes as \$ and leaves it alone in single quotes.

When single quotes are chosen, a string with a line break, a tab or another control character cannot stay in single quotes without changing meaning, because \n would no longer be a line break. Those strings are written in double quotes with the right escape, and every other string keeps its single quotes. The table shows six JSON strings and what each style produces. Accented letters and emoji are written as they are.

The same string in each quote style
JSON stringSingle quotes chosenDouble quotes chosen
"it's"'it\'s'"it's"
"C:\\temp"'C:\\temp'"C:\\temp"
"price: $5"'price: $5'"price: \$5"
"two\nlines""two\nlines""two\nlines"
"tab\there""tab\there""tab\there"
"café 😀"'café 😀'"café 😀"

What a JSON to PHP array converter changes on the way

Two things differ between the formats. PHP has one array type for both lists and maps, so an empty JSON object and an empty JSON array are both [] afterward, and code that must tell them apart needs another marker. PHP also converts a string key that is a plain decimal integer into an integer key, so the keys "12" and "012" in ['12' => 'x', '012' => 'y'] give one integer key and one string key. The page counts such keys and says so, so you know before the file is used.

Integers have a ceiling. The largest PHP integer on a 64-bit system is 9223372036854775807, and a literal above it is read as a float, which loses the last digits. The same goes for -9223372036854775808, because PHP applies the minus after reading the positive number. In [9223372036854775807, 9223372036854775808] the first number is safe and the second is not, and the page adds a warning for it. The counts below come from the sample and show how each JSON type was written.

Invalid JSON is stopped before anything is written. A missing comma, as in {"a":1 "b":2}, gives: Not valid JSON. Line 1, column 8: Expected "," or "}" but found """.

What the sample contains and how PHP names it
JSONPHPCount in the sample
objectarray with string keys3
arrayarray with keys 0, 1, 21
stringstring5
number without a fractionint3
number with a fraction or exponentfloat1
falsefalse1
nullnull1

Use it with

Questions

How do I turn JSON into a PHP array?

Paste the JSON, choose short brackets or array(), and copy the result. Objects become arrays with string keys and JSON arrays become lists.

Is this the same as json_decode?

The result is the same data, but you get source code to paste instead of running json_decode at runtime. It was checked against json_decode with true as the second argument, and the two arrays were identical.

Why does my text end up in double quotes?

A string with a line break, tab or other control character needs an escape such as \n, and escapes only work in double quotes in PHP. All other strings keep single quotes.

What happens to true, false and null?

They become the PHP constants true, false and null, written in lower case.

Can I use the result in a config file?

Yes. Choose the return wrapper and add the opening php tag, save the file and load it with include. It hands back the array.

Is my JSON sent anywhere?

No. The conversion is plain code on this page, and the text you paste is not uploaded.

The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.