Back to Tools

PHP Array to JSON

Convert PHP associative or indexed arrays into JSON objects

PHP Tool
PHP Array Code
JSON Result

                

Change a PHP array literal into JSON

You have a PHP array on screen, maybe from a config file or a var_export dump, and you need the JSON version for an API client, a JavaScript snippet or documentation. Running a script just to call json_encode is a detour when you only need to see the shape of the data. This tool takes an array pasted as text and prints indented JSON with four spaces per level.

Using it

  1. Paste the array into the PHP Array Code box. Load Sample fills in a small user and settings example, and Clear empties the field.
  2. Press Convert to JSON.
  3. Check the JSON Result panel and click Copy to take it away.

When the text cannot be understood, an alert reads Invalid PHP Array structure. Typing in the box hides any earlier result.

How the conversion works

The tool is a text transformation, not a PHP interpreter. It swaps square brackets for braces, replaces the => arrow with a colon, turns single quotes into double quotes and removes a trailing comma before a closing brace, then checks that the outcome parses as JSON. That approach is quick and predictable, but it defines what will and will not work.

What works well

  • Associative arrays where every entry has a key, including nested ones, such as ['user' => 'Ann', 'settings' => ['theme' => 'dark']].
  • Values that are strings, numbers, true, false or null.

What will fail

  • Plain lists such as [1, 2, 3], because the brackets become braces and a JSON object cannot hold bare values. A mixed array like ['status' => 'ok', 'data' => [1, 2, 3]] therefore fails at the inner list.
  • The older array() syntax, variable assignments like $a = [...] and trailing semicolons.
  • Comments, constants, function calls, and strings that contain apostrophes or double quotes.

For those cases, paste only the array literal after removing the surrounding code, quote-fix the strings, and convert list parts separately.

When it is handy

  • Preparing a mock response for a front-end developer from a PHP fixture.
  • Comparing the structure of a config array with the JSON your API returns.
  • Writing documentation examples that show both PHP and JSON forms.
  • Sanity-checking that a keyed array nests the way you expect.

Processing happens through JavaScript in your browser. For production serialisation, always use json_encode inside your PHP application.


Related Tools


Frequently Asked Questions

Why does my array with [1, 2, 3] fail?
The converter turns square brackets into braces, so a list without keys becomes an invalid JSON object. Only arrays where every element has a key convert reliably. Convert list parts separately or rewrite them with keys.
Does it support array() syntax or $variables?
No. It expects a bare array literal in square-bracket form. Remove any $variable = assignment, array() wrappers and the closing semicolon before pasting, or the tool will report an invalid structure.
Is this the same as json_encode?
No. It is a text-based approximation for quick checks, not a PHP engine. Escaping, floats, objects and special characters may differ from json_encode, so use the real function in your application code.
How is the JSON formatted?
The result is pretty-printed with four spaces of indentation per level, ready to read or copy. There is no minify option on this page, so use a separate JSON minifier if you need compact output.
What does the error message mean?
Invalid PHP Array structure means the text still was not valid JSON after conversion. Common causes are unkeyed lists, apostrophes inside strings, missing commas or leftover PHP code around the array.
Support Chat
We typically reply within minutes

Hello! 👋 Before we start, please tell us your name.