Convert JSON to a PHP array

How JSON becomes a PHP array with json_decode, what the conversion changes, where meaning can be lost, and when to keep plain JSON instead.

Converting JSON to a PHP array can mean two different things. One is decoding JSON at runtime so PHP code can work with the data. The other is generating a PHP array literal, source code you paste into a file. Both are covered here because they differ in what they change and in what can go wrong. This overview relies on documented PHP behavior. No specific online converter was evaluated, and no test results or measurements are claimed.

Is converting JSON to a PHP array the right choice?

Decoding JSON into an associative array works well when PHP code needs to read, filter or reshape the data. Arrays work with PHP's array functions and are often simpler to traverse than objects. The main tradeoff is that PHP arrays cannot always keep distinctions that JSON makes. An empty object and an empty array both become an empty PHP array. Numeric-string keys become integer keys. Very large integers can lose precision. If your data has to go back out as JSON in exactly the same shape, check these cases before relying on the conversion.

Generating a PHP array literal suits fixed data that belongs in the codebase: test fixtures, static lookup tables and configuration files that are loaded with include or require. Keep the data as JSON when it is shared with other languages or tools, when it changes often or comes from an external source at runtime, or when people who do not work in PHP need to edit it. In those cases, decode the JSON at runtime instead of hardcoding a converted copy.

What the conversion actually changes

This is a structural conversion between data formats. It does not change any characters for visual effect. PHP's json_decode function parses a JSON string into PHP values. JSON strings, numbers, booleans and null map to PHP strings, integers or floats, booleans and null. JSON arrays become PHP lists with keys numbered from zero. JSON objects become stdClass objects by default. They become associative arrays when you pass true as the second argument, or when you pass the JSON_OBJECT_AS_ARRAY flag.

<?php
$json = '{"name":"Ada","tags":["math","code"],"meta":{}}';

// Objects become associative arrays; throw instead of returning null on bad input
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);

echo $data['tags'][1]; // code

To turn the decoded array into PHP source code, var_export with its second argument set to true returns a valid PHP representation as a string. For the example above, it produces the following. Note that the empty JSON object under meta has become an empty array.

array (
  'name' => 'Ada',
  'tags' => 
  array (
    0 => 'math',
    1 => 'code',
  ),
  'meta' => 
  array (
  ),
)

var_export uses the long array( ) syntax and single-quoted strings. If you want short [ ] syntax, you need your own formatter or a third-party tool. Check what any such tool actually outputs before trusting it.

Where meaning can shift

  • Empty objects and empty arrays: with associative decoding, {} and [] both become an empty PHP array. Re-encoding with json_encode produces [] in both cases, unless you use the JSON_FORCE_OBJECT flag, which changes every array.
  • Numeric-string keys: a JSON key such as "1" becomes the integer key 1, because PHP arrays convert decimal integer strings used as keys into integers.
  • Duplicate keys: when a JSON object repeats a key, only one value survives in the PHP array. Do not depend on duplicate keys.
  • Large integers: integers beyond PHP's integer range are decoded as floats and can lose precision. The JSON_BIGINT_AS_STRING flag keeps them as strings instead.
  • Null versus failure: without error handling, json_decode returns null for invalid input, which looks the same as a valid JSON null. Use JSON_THROW_ON_ERROR (PHP 7.3 and later) or check json_last_error.
  • Encoding: JSON input must be valid UTF-8. Invalid byte sequences cause an error unless you use the JSON_INVALID_UTF8_IGNORE or JSON_INVALID_UTF8_SUBSTITUTE flags, and both of those alter the data.
  • Nesting depth: the default depth limit is 512. Input nested more deeply than that fails to decode unless you raise the limit.
Decode option | Objects become | Typical use
json_decode($json) | stdClass objects | Code that expects property access
json_decode($json, true) | Associative arrays | Array functions, most array-based code
JSON_BIGINT_AS_STRING flag | Unchanged; large integers stay strings | IDs and values beyond integer range
JSON_THROW_ON_ERROR flag | Unchanged; errors raise JsonException | Reliable error handling

If you only need to check that a string is valid JSON without decoding it, PHP 8.3 added json_validate. On earlier versions, decode the string and check for errors.

Will the result stay usable after copying or sharing?

A generated PHP array literal is code, and it only works in a PHP context. To use it, paste it into a file that starts with the PHP opening tag, assign it to a variable or return it, and end the statement with a semicolon. var_export single-quotes strings and escapes backslashes and single quotes, so dollar signs in the data are not treated as variables. If a converter wraps strings in double quotes instead, characters such as dollar signs inside the data can be interpreted by PHP. Review that output before using it.

Once data is hardcoded as a PHP array, JSON tools such as validators, schema checkers and other languages can no longer read it, and search and diff tools see PHP syntax rather than JSON. Large or deeply nested literals are also harder to review than the equivalent pretty-printed JSON. If several systems use the same data, keep the JSON file as the source of truth and decode it or regenerate the array from it, rather than editing two copies by hand.

Accessibility depends on how the result is shown. A converted array in a code editor or documentation page reads like any other code block. Converting the data does not, by itself, make it easier or harder for assistive technology to read. No specific editor, browser or screen reader was tested for this article.

Using online converters

Browser-based JSON-to-PHP converters are convenient for one-off snippets. However, a tool's appearance or marketing tells you nothing about whether pasted data is sent to a server, logged or kept. No converter's privacy or retention practices were verified here. For JSON that contains credentials, personal data or internal configuration, a few lines of local PHP using json_decode and var_export avoid sending the data anywhere. Whichever route you take, compare the output with the input for the edge cases listed above, especially empty objects, numeric keys and large numbers.

Gaps in this overview

This overview describes documented PHP functions and flags. It does not include benchmarks comparing JSON decoding with loading PHP array files, does not review specific converter tools, and does not cover differences between individual PHP releases beyond the version notes above. Check behavior against the PHP version you actually run, because function signatures and flags have changed over time.