Paste a PHP array literal and get valid JSON — associative, indexed, and nested arrays, converted instantly in your browser.
PHP has a single array type that covers both lists and dictionaries, so this tool follows the same rule PHP's own json_encode() uses: if an array's keys are the sequential integers 0, 1, 2, … in order, it converts to a JSON array; otherwise — any string key, non-sequential integer key, or explicit => assignment — it converts to a JSON object. Both the long array(...) syntax and the short [...] syntax are supported, and arrays can be nested to any depth.
| Term | Meaning |
|---|---|
| Indexed array | An array with sequential integer keys starting at 0 — converts to a JSON array [...] |
| Associative array | An array with string keys, or integer keys that skip or start elsewhere — converts to a JSON object {...} |
| Nested array | An array used as a value inside another array — converted recursively, preserving structure |
| Pretty-print / Minify | Whether the JSON output is indented for readability or written as compact single-line JSON |
The PHP Array to JSON Converter is an online developer tool that transforms PHP array syntax into valid JSON. Whether you're working with associative arrays, indexed arrays, multidimensional arrays, or nested data structures, this converter automatically parses your PHP array and generates standards-compliant JSON that can be used in APIs, JavaScript applications, databases, configuration files, and third-party integrations.
PHP arrays are one of the most commonly used data structures in web development. However, many frontend frameworks, REST APIs, mobile applications, and cloud services expect data in JSON format. Instead of manually rewriting array structures, this converter performs the conversion instantly while preserving nested objects, arrays, numbers, booleans, strings, and null values.
Everything runs directly inside your browser, so your source code remains private. Simply paste your PHP array, choose whether to generate pretty-printed or minified JSON, and copy or download the result.
JSON has become the standard format for exchanging structured data across different programming languages and platforms. Converting PHP arrays into JSON makes your data portable, readable, and compatible with modern applications.
.json file.Suppose you have the following PHP array:
$user = array(
"name" => "John Doe",
"age" => 32,
"active" => true,
"roles" => array("Admin", "Editor")
);
The converter generates the following JSON:
{
"name": "John Doe",
"age": 32,
"active": true,
"roles": [
"Admin",
"Editor"
]
}
This JSON can now be used directly in REST APIs, JavaScript applications, AJAX responses, mobile apps, and cloud services without any additional formatting.
json_encode() in PHP
The json_encode() function is PHP's built-in method for converting PHP values into JSON. It supports arrays, associative arrays, objects, strings, numbers, booleans, and null values, producing standards-compliant JSON that can be exchanged with JavaScript applications, REST APIs, mobile apps, and third-party services.
Our PHP Array to JSON Converter follows the same conversion rules as json_encode(), making it an excellent way to preview and validate JSON before integrating it into your application.
$user = [
"name" => "John Doe",
"age" => 30,
"active" => true
];
echo json_encode($user);
Output:
{"name":"John Doe","age":30,"active":true}
If you need readable JSON during development, you can enable pretty printing.
echo json_encode($user, JSON_PRETTY_PRINT);
After converting a PHP array into JSON, many developers eventually need to convert that JSON back into a PHP array. PHP provides the json_decode() function for this purpose.
$json = '{"name":"John Doe","age":30}';
$array = json_decode($json, true);
print_r($array);
Passing true as the second parameter tells PHP to return an associative array instead of a PHP object.
Understanding both json_encode() and json_decode() makes it easier to exchange data between backend PHP applications and frontend JavaScript frameworks.
During conversion, PHP automatically maps its native data types to their closest JSON equivalents. Understanding these mappings helps avoid unexpected output when generating API responses or configuration files.
| PHP Data Type | JSON Output | Example |
|---|---|---|
| String | String | "John" |
| Integer | Number | 25 |
| Float | Number | 99.95 |
| Boolean | true / false | true |
| null | null | null |
| Indexed Array | JSON Array | [ ] |
| Associative Array | JSON Object | { } |
These mappings follow the official behavior of PHP's json_encode() function, ensuring compatibility with modern APIs and JavaScript applications.
An indexed array contains sequential numeric keys beginning with zero.
$colors = [
"Red",
"Green",
"Blue"
];
JSON Output
[
"Red",
"Green",
"Blue"
]
Associative arrays use named keys and become JSON objects.
$user = [
"name" => "Alice",
"country" => "Canada"
];
JSON Output
{
"name":"Alice",
"country":"Canada"
}
Nested arrays are converted recursively while preserving the original hierarchy.
$users = [
[
"name"=>"John",
"age"=>30
],
[
"name"=>"Jane",
"age"=>28
]
];
The generated JSON maintains the same nested structure, making it suitable for REST APIs, configuration files, and frontend applications.
PHP Array to JSON conversion is widely used across modern web development. JSON serves as the standard format for exchanging structured data between different systems, making this conversion essential in many real-world scenarios.
If your converter reports a parsing error, first validate the PHP array syntax. Most issues are caused by a small syntax mistake such as a missing comma or bracket.
A PHP Array to JSON Converter is an essential utility for PHP developers, API engineers, backend programmers, and anyone working with structured data. Instead of manually rewriting arrays into JSON format, you can instantly generate clean, standards-compliant JSON while preserving nested objects, arrays, and data types.
Whether you're building REST APIs, creating configuration files, debugging applications, or integrating third-party services, this tool simplifies the conversion process, reduces manual errors, and improves productivity. Since the conversion happens directly in your browser, it's also a fast and privacy-friendly solution for everyday development tasks.
null values while preserving the overall data structure.array() syntax and the short [] syntax are supported.json_encode() is a built-in PHP function that converts PHP arrays, objects, strings, numbers, booleans, and null values into valid JSON. It is commonly used when creating REST APIs, sending AJAX responses, and exchanging data between PHP and JavaScript applications.
json_decode() is the opposite of json_encode(). It converts a JSON string back into a PHP object or associative array. Passing true as the second parameter returns the result as an associative array instead of a PHP object.
null values are preserved during conversion and become JSON null values. This ensures missing or optional data remains correctly represented in the generated JSON.
true becomes JSON true, while false remains false, ensuring compatibility with JavaScript and API consumers.
json_decode() function with its second parameter set to true. This makes it easy to read JSON received from APIs or external services.
JSON_UNESCAPED_UNICODE to keep Unicode characters readable in the generated JSON.
json_encode() behavior. If an array has sequential numeric keys starting from 0, it becomes a JSON array ([]). If it contains string keys or non-sequential numeric keys, it is encoded as a JSON object ({}).