About PHP Array Generator
Paste any JSON object and get valid PHP array syntax. Handles nested objects (as nested arrays), arrays, strings (single-quoted), numbers, booleans, and null. The output uses PHP short array syntax ([]) and follows PSR-2 indentation.
Frequently Asked Questions
What is JSON to PHP conversion?
JSON to PHP conversion transforms a JSON object or array into PHP array syntax, producing code you can paste directly into a PHP file. Every JSON key becomes an array key, nested objects become nested arrays, and all types map to their PHP equivalents.
What is the difference between json_decode returning an array vs an object?
json_decode($json) returns a stdClass object by default, so you access fields with ->: $obj->name. json_decode($json, true) returns an associative array, so you use square brackets: $arr["name"]. The associative array form is more common in modern PHP since it composes naturally with array functions and avoids stdClass gotchas.
How do I convert JSON to a PHP associative array using json_decode?
Pass true as the second argument: $data = json_decode($json, true). Check json_last_error() === JSON_ERROR_NONE to confirm parsing succeeded. For deeply nested JSON, the result is a nested associative array — access with $data["user"]["address"]["city"].
Can I convert a PHP array back to JSON with json_encode?
Yes: echo json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE). JSON_PRETTY_PRINT adds indentation; JSON_UNESCAPED_UNICODE preserves non-ASCII characters without escaping. For API responses, omit JSON_PRETTY_PRINT to minimise payload size.
How do I handle deeply nested JSON in PHP arrays?
Access nested values with chained square brackets: $city = $data["user"]["address"]["city"]. Use the null-coalescing operator for safe access: $city = $data["user"]["address"]["city"] ?? "unknown". For complex deep access, consider the array_key_exists() check or a library like adbario/php-dot-notation that supports dot-notation paths.
Advertisement300 × 250
Advertisement300 × 250