PHP Array to JSON
Convert PHP associative or indexed arrays into JSON objects
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
- Paste the array into the PHP Array Code box. Load Sample fills in a small user and settings example, and Clear empties the field.
- Press Convert to JSON.
- 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