Developer Tools

JSON to PHP Array

Turn JSON into clean PHP source code. Choose the short [ ] syntax, classic array( ) or var_export style, wrap the result in return for a config file or assign it to a variable, align the => arrows, and decide whether JSON objects become associative arrays or stdClass objects. Strings are safely single-quoted, numbers and booleans keep their types, and notes explain where JSON and PHP behave differently.

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

How to use JSON to PHP Array

  1. Paste or load JSON.
  2. Choose the array syntax and how to wrap it.
  3. Decide how JSON objects should appear in PHP.
  4. Copy the PHP code or download it as config.php.

JSON to PHP Array features

Three syntaxes

Short [ ], array( ) or var_export style.

Config-file ready

return [ … ]; with <?php and strict types, or $data = [ … ];

Objects your way

Associative arrays like json_decode($json, true), or (object) casts.

Safe strings

Single quotes with \ and ' escaped; no accidental interpolation.

Readable output

Four-space indentation, trailing commas and aligned arrows.

Precision warnings

Flags very large integers and empty objects.

When to use JSON to PHP Array

  • Creating a PHP config file from a JSON settings file.
  • Writing PHPUnit fixtures from a recorded API response.
  • Hard-coding a lookup table that arrived as JSON.
  • Porting Laravel or Symfony configuration between formats.

JSON to PHP Array FAQ

Is this the same as json_decode?

The result is the same data that json_decode($json, true) returns, written out as PHP source code so you can keep it in a file.

Why single quotes?

Single-quoted PHP strings never interpolate variables, so a $ in your data cannot accidentally change it. Only backslashes and single quotes need escaping.

What happens to empty JSON objects?

With associative arrays they become [], which json_encode turns back into [] instead of {}. Choose (object) casts if the distinction matters.

Are large numbers safe?

JSON integers above 9,007,199,254,740,991 lose precision when read by JavaScript, so they may already be rounded. PHP can hold larger integers; keep such IDs as strings in the JSON.

Can I convert PHP arrays back to JSON?

Yes, with the PHP Array to JSON tool, which also reads var_export() and print_r() output.

Is my data uploaded?

No. The conversion runs in your browser.

Writing JSON data as PHP code

PHP applications often keep configuration and fixed data in PHP files that simply return an array. Such files are fast, because PHP caches them as compiled code, and they can hold comments. When the data starts life as JSON, for example exported from another system or copied from an API response, converting it by hand is tedious and error-prone. This tool writes the array for you.

JSON objects and arrays both become PHP arrays by default, matching json_decode with its second argument set to true. Lists keep implicit keys, so they read naturally, and objects become associative arrays with quoted string keys. If your code works with objects instead, the (object) cast option produces stdClass instances, matching json_decode without the second argument.

Strings are written in single quotes, where only the backslash and the single quote have special meaning. That avoids a trap of double-quoted strings: a dollar sign followed by letters would be read as a variable. Numbers keep their form, including decimals such as 2.0 so that they stay floats in PHP, and true, false and null are written in lower case.

The var_export style mirrors what PHP’s own var_export() function prints: explicit keys for every element, including lists, and nested arrays starting on their own line. It is useful when the output will be compared with real var_export() output. The short syntax with aligned arrows is easier to read and is the usual style in modern code bases.

Two differences between JSON and PHP are worth knowing. Empty JSON objects have no distinct PHP array form, so they come back as [] when encoded again unless they are objects. And JSON numbers are parsed by JavaScript in the browser, which rounds integers beyond 2^53; the converter warns when the input contains such numbers.

Other useful tools