Skip to main content
Programming Language Tools

JSON to PHP Array

Convert JSON into an equivalent PHP array literal, ready to paste directly into PHP code.

Paste JSON below - this tool converts it into an equivalent PHP array literal you can paste directly into your code.

Going the other direction? Use the PHP Array to JSON converter instead.

What Does This Tool Do?

It parses JSON and generates the equivalent PHP array literal using the modern short array syntax (['key' => 'value']), correctly handling nested objects and arrays.

Why Use This Tool?

Useful when you have a JSON API response or config file and want to hardcode the equivalent data directly as a PHP array - for a test fixture, a config file, or a quick script.

How to Use It

  1. Paste valid JSON.
  2. Click Convert.
  3. Copy the generated PHP array code.

Limitations

JSON's data types (string, number, boolean, null, array, object) map directly to PHP equivalents - there's no ambiguity in this direction, unlike JSON-to-typed-language conversions that need to infer types.

Example

{"name": "Blue Mug", "price": 249, "in_stock": true, "tags": ["kitchen", "gift"], "discount": null}

becomes:

$data = [
    'name' => 'Blue Mug',
    'price' => 249,
    'in_stock' => true,
    'tags' => [
        'kitchen',
        'gift',
    ],
    'discount' => null,
];

Where It Helps

  • Laravel and Symfony config files, which return PHP arrays.
  • PHPUnit or Pest tests - paste an API response as expected data.
  • Seeders and factories that need realistic sample records.

Alternative: Decode at Runtime

If the JSON changes often or is large, keep it as a file and decode it instead:

$data = json_decode(file_get_contents(__DIR__.'/data.json'), true, flags: JSON_THROW_ON_ERROR);

The true argument returns arrays rather than objects. A hard-coded array is better for small, stable data that you want to read and edit in PHP.

Convert back with PHP Array to JSON, and tidy the file with the PHP Formatter.

Frequently Asked Questions

Yes, the output uses modern [ ] syntax, ready to paste into PHP 5.4 or newer.

null becomes null, and true/false stay true/false - the PHP equivalents.

Yes, at runtime json_decode($json, true) gives the same array. This tool is for when you want the array written directly in source code, such as config files or tests.
Share this tool: