JSON to PHP Array Converter

Parses JSON and renders it as PHP source: a $data = [...]; array literal using short-array syntax, 'key' => value entries, and single-quoted strings, so a JSON payload can be dropped straight into a PHP script or test fixture. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-24

Overview

Introduction

Pasting JSON test data or config into a PHP script usually means hand-converting it into an array() or [...] literal by hand.

This tool does that conversion automatically, producing ready-to-paste PHP source with correct short-array syntax and properly escaped strings.

What Is JSON to PHP Array Converter?

A converter that parses JSON and recursively renders the resulting value as a PHP array literal: JSON objects become 'key' => value entries, JSON arrays become positional entries, and scalars map to PHP's null, true/false, numeric, and single-quoted string literals.

The output is wrapped in a full <?php $data = [...]; statement so it's directly usable as a PHP file or snippet.

How JSON to PHP Array Converter Works

After parsing the JSON, the tool recurses through the value: objects render as [...] blocks with 'key' => value lines indented four spaces deeper per level, arrays render the same way but with bare positional values, and scalars render via a small mapping (null, true/false, numbers as-is, strings single-quoted with \\ and \' escaped).

Empty objects and empty arrays both render as [], since PHP's array type doesn't distinguish an empty list from an empty map, only the entries (if any) determine which one you end up with when parsed back.

When To Use JSON to PHP Array Converter

Use it when you have JSON sample data, an API response, a fixture, and need it as native PHP array code for a test or config file.

It's also handy for quickly seeing how a JSON structure would look expressed in PHP's array syntax.

Features

Advantages

  • Produces syntactically valid modern PHP (short-array syntax) that can be pasted directly into a .php file.
  • Correctly distinguishes object entries (with 'key' =>) from array entries (bare values) based on the JSON input's own shape.
  • Escapes strings safely for PHP's single-quoted string literal rules.

Limitations

  • PHP has no single canonical array-literal parser to invert this against in general PHP code; this site's own PHP Array to JSON tool only understands the specific subset this converter produces.
  • Empty JSON objects and empty JSON arrays both render as the same [] literal, so that distinction is lost in the PHP output.

Examples

Nested object with an array value

Input

{"name":"Ada","roles":["admin","editor"]}

Output

<?php

$data = [
    'name' => 'Ada',
    'roles' => [
        'admin',
        'editor',
    ],
];

The object becomes 'key' => value entries; the nested array becomes bare positional entries.

Best Practices & Notes

Best Practices

  • Review generated keys and strings before committing them to source control, especially if the original JSON came from an untrusted source.
  • Use PHP Array to JSON to double-check a round trip before relying on hand-edited output.
  • For very large JSON payloads, consider keeping the data as a JSON file loaded at runtime instead of inlining it as PHP source.

Developer Notes

The render() function mirrors the one already used by this site's XML to PHP Array converter almost exactly, the only difference is the parsing step in front of it (JSON.parse here versus an XML-to-JSON pass there), which keeps PHP array rendering behavior consistent across every format this site converts into PHP.

JSON to PHP Array Converter Use Cases

  • Turning an API response's JSON sample into a PHP test fixture
  • Converting a JSON config into a PHP config array for a legacy codebase
  • Quickly previewing how nested JSON would look as PHP array syntax

Common Mistakes

  • Assuming the output round-trips through arbitrary PHP array code; only this converter's own PHP Array to JSON tool is guaranteed to parse it back.
  • Not noticing that an empty {} and an empty [] both produce the same [] in the PHP output.
  • Forgetting the output includes a full <?php opening tag and $data = assignment, which may need trimming if pasting into an existing file.

Tips

  • Copy just the array literal (skip the <?php and $data = wrapper) if you're inserting it into an existing PHP expression.
  • Use PHP Array to JSON afterward to sanity-check that the generated array round-trips to the JSON you expect.

References

Frequently Asked Questions