Back to Tools

JSON to PHP Array

Convert JSON strings into PHP array syntax (associative or indexed)

PHP Tool
Source JSON
PHP Array Output

                

Convert JSON into PHP array code

API responses and exported settings usually come as JSON, but PHP projects often want the same data as a native array: a Laravel config file, a test fixture, a seed list or a mock response. Rewriting braces, colons and quotes by hand is tedious and easy to get wrong. This tool takes a JSON string and prints it as PHP array syntax using square brackets and the familiar arrow between keys and values.

How to convert

  1. Paste your JSON into the Source JSON box. Use Load Sample to see an example object, or Clear to empty the field.
  2. Click Convert to PHP Array.
  3. Read the result in the PHP Array Output panel and press Copy to put it on your clipboard.

If the text is not valid JSON, an alert says so and nothing is produced. Editing the input hides the previous result, so you never copy stale output.

What the output looks like

  • Objects and lists become nested [ ... ] blocks indented by four spaces per level.
  • Every key is written in single quotes followed by =>, with a trailing comma after each item except the last.
  • Strings are wrapped in single quotes; numbers and true or false are written as they are; null becomes null.

Where it saves time

  • Copying a sample API payload into a PHPUnit test as an expected array.
  • Turning a JSON settings export into a config file.
  • Building seed data for a database seeder from an existing dataset.
  • Documenting a request body in PHP code examples.

Things to check in the result

The converter handles values simply, so a few points deserve a glance. Text that contains a single quote or a backslash is not escaped, meaning a value such as O'Brien needs a manual backslash before the apostrophe. JSON lists are written with their index as a quoted key, for example '0' => 'first', rather than as a bare list; PHP treats numeric string keys like integer keys, so lookups still work, but you may prefer to delete the keys by hand for tidier code. Very large numbers follow JavaScript rounding rules before they reach the output.

Everything is processed by JavaScript in your browser after you press the button. Review the code before pasting it into a project, especially for strings with special characters.

Related Tools


Frequently Asked Questions

What PHP syntax does the tool produce?
It uses the short array syntax with square brackets, single-quoted keys and the => operator, indented four spaces per nesting level. It does not output the older array() style. Nested levels are indented consistently, so the result can be pasted straight into a file.
Are apostrophes in my strings escaped?
No. Values are placed between single quotes exactly as they appear, so a string containing an apostrophe or backslash must be fixed by hand, otherwise the PHP will not parse. Check text-heavy data before use.
How are JSON arrays converted?
Each item is written with its position as a quoted key, such as '0' => 'value'. PHP converts numeric string keys to integers, so behaviour matches a normal list, but you can strip the keys manually if you want a plain list.
What if my JSON is invalid?
The tool shows an alert saying the JSON format is invalid and produces no output. Look for trailing commas, single-quoted strings or missing braces, fix them and press Convert to PHP Array again.
Is my data sent to a server?
The conversion is done by JavaScript running in your browser when you click the button; the page does not submit your input to a server route for this step. You can still copy the result with the Copy button.
Support Chat
We typically reply within minutes

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