PHP Array to JSON Converter (Free AI Tool)
Convert PHP array() and [] syntax to JSON format instantly. Transform Laravel collections, Symfony configuration arrays, and associative arrays to JSON for REST API responses. Supports both old and modern PHP array syntax.
Paste code in both editors to see differences
Hint: Paste original code on left, modified code on right, then click Compare to see differences highlighted.
Hint: Paste your code, customize font size and line numbers, then click Export PDF to download formatted code.
Hint: Paste your JWT token to decode and view its header, payload, and signature. The tool validates token structure and format.
Hint: Select conversion type, paste your data, and get instant conversion. Supports JSON, YAML, XML, Excel, PDF, and more.
How It Works
- 1
Paste PHP Array with => Operators
Paste your PHP array code using array() or [] syntax including associative arrays with => operators from Laravel responses, Symfony configs, or WordPress data structures.
- 2
AI Converts PHP Syntax to JSON
The tool parses PHP array syntax, converts => operators to JSON colons, transforms single quotes to double quotes, and determines whether structures become JSON objects or arrays based on key types.
- 3
Copy JSON for API Responses
Receive JSON output ready for REST API responses, AJAX calls, or front-end JavaScript consumption from PHP backends using Laravel or vanilla PHP.
PHP Array vs JSON: Syntax Comparison
| Feature | PHP Array | JSON |
|---|---|---|
| Syntax | [] or array() | {} and [] |
| Key-Value | => operator | : colon |
| Associative | String keys | Object properties |
| Indexed | Numeric keys | Arrays |
| Use Case | Laravel/Symfony backend | REST API responses |
| Trailing Comma | Allowed in PHP 7.3+ | Not allowed in JSON |
Code Examples
Example 1: Associative Array from Laravel
[
'user_id' => 101,
'username' => 'john_doe',
'email' => '[email protected]',
'active' => true,
'roles' => ['admin', 'editor']
] {
"user_id": 101,
"username": "john_doe",
"email": "[email protected]",
"active": true,
"roles": ["admin", "editor"]
} Key Changes:
PHP associative array with string keys (user_id, username) converts to JSON object. The => operators become : colons. Single quotes in PHP become double quotes in JSON (JSON spec requires double quotes). Boolean true remains true. The nested numeric array roles converts to JSON array. This is the standard Laravel API response format where Eloquent models return associative arrays via toArray() method, ready for json_encode() or direct API consumption.
Example 2: Old-Style array() Syntax
array(
'products' => array(
array(
'id' => 'P001',
'name' => 'Laptop',
'price' => 999.99,
'in_stock' => true
),
array(
'id' => 'P002',
'name' => 'Mouse',
'price' => 29.99,
'in_stock' => false
)
)
) {
"products": [
{
"id": "P001",
"name": "Laptop",
"price": 999.99,
"in_stock": true
},
{
"id": "P002",
"name": "Mouse",
"price": 29.99,
"in_stock": false
}
]
} Key Changes:
Old-style array() notation converts identically to modern [] syntax. The outer associative array becomes JSON object. The nested numeric-indexed array of products becomes JSON array. Each product associative array becomes JSON object within the array. Floats (999.99) and booleans preserve types. This handles legacy PHP codebases and WordPress plugins still using array() syntax while producing modern JSON for REST APIs compatible with fetch() in JavaScript or requests in Python.
Frequently Asked Questions
Associative PHP arrays with string keys convert to JSON objects. For example, ['name' => 'John', 'age' => 30] becomes {"name": "John", "age": 30}. Numeric-indexed arrays become JSON arrays. Mixed arrays (both numeric and string keys) convert to JSON objects preserving all keys.
Yes. The converter handles both old-style array() notation from PHP 5.3 and modern short [] syntax from PHP 5.4+. Both ['a', 'b'] and array('a', 'b') convert identically to JSON ["a", "b"]. The tool auto-detects syntax.
PHP types convert to JSON equivalents: strings become JSON strings, integers/floats become numbers, true/false/null become JSON primitives. PHP objects and resources cannot convert as JSON lacks equivalents. The converter focuses on array structures used in Laravel responses and API data.