JSON to PHP Array

The JSON to PHP Array Converter transforms JSON objects and arrays into PHP associative array syntax ready to paste into any PHP file. Use it to create PHPUnit test fixtures, Laravel database seeders, WordPress plugin configuration arrays, and static PHP data files from JSON sources.

S. Siddiqui

Edited by

S. SiddiquiFounder & Editor-in-Chief
Sources:MDN Web DocsW3CIETFUpdated Jun 2026
$

What Is the JSON to PHP Array Converter?

The JSON to PHP Array Converter transforms a JSON object or array into PHP array syntax — the associative array and indexed array structures used natively in PHP. A JSON object such as {"name": "Alice", "age": 30} becomes a PHP associative array: ['name' => 'Alice', 'age' => 30]. A JSON array such as [1, 2, 3] becomes a PHP indexed array: [1, 2, 3]. Nested JSON objects and arrays are recursively converted, producing correctly nested PHP array syntax that can be pasted directly into any PHP file.

PHP developers regularly need to work with JSON data — from REST API responses, configuration files, third-party service webhooks, and test fixtures. The standard way to convert JSON to a PHP array at runtime is json_decode($jsonString, true), where the second argument true ensures the result is an associative array rather than a stdClass object. However, there are many scenarios where you want the PHP array as static code: seeding a configuration file, creating a test fixture, pasting API response data into a PHPUnit test, or generating a PHP data file from an external JSON source.

According to the PHP manual for json_decode(), passing true as the second argument returns an associative array rather than an object. This tool produces the same structure as static PHP array syntax — the code equivalent of what json_decode($json, true) would return at runtime, written out as literal PHP code you can paste into a file.

This generator is used by PHP developers creating test fixtures with realistic data, WordPress plugin developers working with API response data, Laravel developers seeding databases from JSON exports, and PHP developers who need to convert API documentation examples into PHP code they can paste into their projects.

How to Use the JSON to PHP Array Converter

  1. Paste your JSON. The input accepts any valid JSON: objects, arrays, nested structures, and primitive values. If the JSON is not valid, the tool will indicate the error before attempting conversion.
  2. Choose your array syntax. Select Short syntax ([...]) for modern PHP 5.4+ projects — this is the standard in Laravel, Symfony, and modern PHP codebases. Select Long syntax (array(...)) for legacy PHP 5.3 or earlier projects.
  3. Review the output. PHP strings use single quotes by default in the output. Numeric values are unquoted. Booleans appear as true or false. JSON null becomes PHP null. Nested objects become nested associative arrays; nested JSON arrays become indexed arrays.
  4. Copy the output. Paste the PHP array directly into your PHP file — inside a variable assignment, a function argument, a test fixture, or a configuration array.
  5. Use in your PHP code. The generated array is valid PHP syntax ready for direct use: $data = [/* generated array */];

Why Use This Tool

Converting JSON to PHP array syntax by hand is tedious for any structure beyond a few key-value pairs. Nested objects require careful brace and bracket matching, numeric values must be distinguished from string values, and PHP's arrow operator (=>) syntax differs from JSON's colon syntax. Even small transcription errors produce PHP parse errors that can be difficult to trace. This tool handles all of it automatically.

PHPUnit and Pest test fixtures

PHP unit and feature tests frequently use inline data arrays as expected values or request payloads. When the data comes from a real API response documented in JSON, converting it to a PHP array fixture manually is error-prone. This tool converts the JSON into a PHP array you can paste directly into a $expected variable or a data provider array in your test class, giving you a realistic fixture in seconds.

Laravel database seeders

Laravel database seeders use PHP arrays to define seed data. When seed data originates from a JSON export — a CRM export, a product catalogue, a user directory — converting it to PHP array syntax allows you to paste it directly into a seeder's DB::table()->insert() call or an Eloquent model's create() method. This is faster than loading JSON files at runtime and produces self-contained seeder classes.

WordPress plugin developers

WordPress plugins frequently work with configuration data, API responses, and structured content. PHP arrays are the standard data structure in WordPress — options, transients, and post meta all use PHP arrays. A WordPress developer who receives a JSON configuration sample from a design specification or a third-party service can convert it to PHP array syntax and paste it into their plugin's configuration files or option defaults.

Generating PHP configuration from JSON

Many PHP frameworks use PHP array files for configuration — Laravel's config/ files, Symfony's parameter arrays, and CodeIgniter's config files all use PHP arrays. When configuration data is authored in JSON (by a non-PHP team, a design tool, or a third-party service), this converter produces the PHP array syntax that PHP configuration files expect.

Real-World Use Cases

Laravel developer creating a database seeder from a JSON export

A Laravel developer receives a CSV export of 500 product records, converted to JSON by a data analyst. To seed a development database, the developer needs a Laravel seeder with the data as PHP arrays. They paste the JSON into this converter, copy the PHP array output, and insert it into the seeder's DB::table('products')->insert([...]) call. The seeder populates the development database in seconds and is committed to the repository so every team member can run it. The conversion takes under a minute compared to the manual alternative of transcribing 500 records from JSON to PHP syntax.

PHPUnit developer creating a test fixture from an API response

A developer writes a PHPUnit feature test for a REST API endpoint that processes Stripe webhook events. The test needs a realistic webhook payload as its input. Rather than fabricating test data, the developer captures a real test event from the Stripe CLI, pastes it into this converter, and copies the PHP array into the test class as the $payload variable. The test is now backed by a real payload structure, making it far more representative than hand-crafted test data.

WordPress plugin developer working with a third-party API

A WordPress plugin developer integrates a weather API that returns JSON responses. During development, the developer wants to test the plugin's output rendering without making live API calls. They capture a real API response, convert it to PHP array syntax using this tool, and use it as the mock return value in their unit test. This allows the rendering logic to be tested independently of the API, making tests faster and more reliable.

PHP developer converting API documentation examples

A developer integrates a shipping carrier's API into a PHP e-commerce platform. The API documentation includes JSON examples for each endpoint's request and response. To understand the PHP structure they will work with at runtime, the developer pastes each JSON example into this converter and sees the equivalent PHP array. This confirms their assumptions about how json_decode($response, true) will structure the data before writing the integration code, preventing misunderstandings that only surface in testing.

Common Mistakes and Troubleshooting

Confusing json_decode with and without the associative argument

PHP's json_decode($json) returns a stdClass object by default, where properties are accessed with arrow notation: $data->name. json_decode($json, true) returns an associative array where values are accessed with bracket notation: $data['name']. This tool produces the associative array form (equivalent to passing true). If your code uses object notation, either pass false or omit the second argument to json_decode() at runtime, or cast the array: (object) $array.

Integer keys in PHP arrays from JSON arrays

JSON arrays (starting with [) produce PHP indexed arrays with numeric keys starting at 0. PHP associative arrays (from JSON objects starting with {) use string keys with the => operator. If you access a JSON array element by key in the PHP output, use numeric index notation: $array[0]. Mixing up object and array syntax is a common source of "undefined index" notices in PHP.

Pasting the PHP output without a variable assignment

The generated PHP array syntax is just the array literal — it is not a complete PHP statement. To use it, assign it to a variable: $data = [/* generated array */]; or pass it directly as a function argument. Pasting the raw array literal without assignment produces a PHP parse error. The tool may include a variable assignment in the output — check and adjust the variable name to match your codebase's naming conventions.

Handling very large integers

PHP integers are 64-bit on 64-bit systems (maximum value ~9.2 × 10^18). JSON integers within this range convert correctly to PHP int. However, some APIs return IDs as very large numbers (social platform IDs, database row IDs on large systems) that may exceed PHP's integer range on 32-bit systems or match JavaScript's Number precision limit. In these cases, the JSON will contain the value as a string: "id": "9007199254740993". If the JSON has the value as a bare integer and it exceeds PHP's range, it will be converted to a float in the PHP output — use string representation instead.

PHP reserved words as array keys

PHP reserved words (class, function, list, match, default, etc.) can be used as array keys without issue: ['class' => 'User'] is valid PHP. However, if you convert the array into an object property, reserved words as property names require quoted access: $obj->{'class'}. This is an edge case, but be aware of it if you plan to convert the PHP array into an object.

Last reviewed: June 7, 2026

Frequently Asked Questions

What is the difference between json_decode with true and without?
json_decode($json) returns a stdClass object where properties are accessed with arrow notation: $data->name. json_decode($json, true) returns an associative array where values are accessed with bracket notation: $data['name']. This tool produces the associative array form (equivalent to passing true). If you need object access, use json_decode without the second argument, or cast the array to an object with (object) $array.
Can I paste the PHP array directly into a Laravel config file?
Yes. Laravel configuration files (config/*.php) return PHP arrays. Paste the generated array as the return value: return [/* generated array */]; The array keys become configuration keys accessible via config('filename.key'). For nested arrays, use dot notation to access nested values: config('services.stripe.key'). Make sure the top-level array structure matches what your application code expects — Laravel config files should have intentional, documented structures.
How are JSON null values represented in PHP?
JSON null converts to PHP null. In the PHP array output, null appears as the bare keyword null without quotes: ['email' => null]. PHP null is falsy and distinct from an empty string (''), zero (0), and false. When working with a PHP array containing null values, use isset($array['key']) to check if a key exists and is non-null, or array_key_exists($key, $array) to check if the key exists regardless of its value.
What is the difference between short and long PHP array syntax?
Short syntax uses square brackets: ['key' => 'value']. This is available from PHP 5.4+ and is the standard in modern PHP codebases and frameworks including Laravel and Symfony. Long syntax uses the array() constructor: array('key' => 'value'). This is compatible with PHP 5.3 and earlier. Both produce identical results. Use short syntax for any PHP 5.4+ project — it is cleaner, more readable, and universally adopted in modern PHP development.
How do I use the PHP array with Eloquent in Laravel?
To create a model instance from the generated array: User::create(['name' => 'Alice', 'age' => 30]). For batch inserts into a table: DB::table('users')->insert([['name' => 'Alice', 'age' => 30], ['name' => 'Bob', 'age' => 25]]). For updating: User::where('id', 1)->update(['name' => 'Alice']). The generated array keys must match the model's $fillable attributes or database column names for Eloquent operations to succeed.
How do I convert a PHP array back to JSON?
Use json_encode($phpArray): $jsonString = json_encode($phpArray). For pretty-printed output: $jsonString = json_encode($phpArray, JSON_PRETTY_PRINT). For UTF-8 safety: $jsonString = json_encode($phpArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT). json_encode() is the reverse of json_decode() — the conversion is lossless for standard PHP arrays containing strings, numbers, booleans, null, and nested arrays.
What PHP version is required for json_decode?
json_decode() has been available since PHP 5.2. The associative argument (the second true/false parameter) was added in PHP 5.2 as well. The depth limit parameter was formalised in PHP 5.3. The flags parameter (including JSON_THROW_ON_ERROR for exception-based error handling rather than returning null on failure) was added in PHP 7.3. For modern PHP, use json_decode($json, true, 512, JSON_THROW_ON_ERROR) to get an exception on malformed JSON rather than a silent null.
How do I handle JSON with integer keys in PHP?
JSON arrays (starting with [) produce PHP indexed arrays with integer keys 0, 1, 2, etc. JSON objects (starting with {) produce PHP associative arrays with string keys. A JSON object with integer-looking string keys like {"0": "a", "1": "b"} produces a PHP associative array with string keys: ['0' => 'a', '1' => 'b']. PHP treats these as strings, not integers. If you need integer keys, cast them: array_map(fn($k) => (int)$k, array_keys($array)).
How do I use the generated array in a PHPUnit test?
Paste the generated PHP array as a variable in your test method: $expected = [/* generated array */]; or as a data provider return value. For testing API responses, compare the generated array against the actual parsed response: $this->assertEquals($expected, json_decode($response->getContent(), true)). For request bodies in Laravel feature tests: $response = $this->postJson('/api/users', ['name' => 'Alice', 'age' => 30]). The generated array ensures the test data matches the exact JSON structure your code will process.
Can I use the PHP array to mock an API response in tests?
Yes. In PHPUnit with Mockery or PHPUnit's built-in mock builder, you can configure a mock HTTP client to return the PHP array when a specific endpoint is called. In Laravel, use Http::fake(['api.example.com/*' => Http::response(['name' => 'Alice', 'age' => 30], 200)]) to fake a specific API response with the array as the body. The generated PHP array is the starting point for any mock response that should match a real API's JSON structure.

Rate This Tool

Was this tool helpful?

Be the first to rate this tool

About the Author

S. Siddiqui

S. Siddiqui

Founder & Editor-in-Chief

LinkedIn Profile

S. Siddiqui is the founder and editor-in-chief of YourToolsBase, overseeing all content, tool accuracy, and editorial standards.

View full profile

Authoritative Sources

Formulas and data in this tool are based on guidelines from the above sources.