JSON to PHP Array Converter

JSON Input
1
Variable Name
PHP Array Output

Last updated:

Jsonic's JSON to PHP Array converter outputs PHP associative array syntax from any JSON object. JSON strings become single-quoted PHP strings, integers and floats become PHP number literals, booleans become true/false, and null stays null. JSON arrays become PHP array literals. Nested JSON objects become nested PHP arrays with the same key names — no class definitions or type annotations are needed. The output uses short array syntax (square brackets) with trailing commas and 4-space indentation. All processing runs in your browser with no data upload.

How to convert JSON to a PHP array

  1. Paste your JSON into the left panel, or click Example to load a sample.
  2. Optionally change the variable name (default: $data).
  3. Click Generate.
  4. Copy the output and paste it into your PHP file.
  5. Use json_decode($jsonString, true) in production code to decode JSON at runtime.

FAQ

What is a PHP associative array?

A PHP associative array is a key-value map where keys are strings and values can be any type — strings, integers, floats, booleans, null, or nested arrays. It is the PHP equivalent of a JSON object. PHP uses the same array type for both indexed arrays (numeric keys) and associative arrays (string keys), distinguished by how they are created and accessed.

How do I decode JSON in PHP natively?

Use json_decode($jsonString, true) — the second argument true makes PHP return an associative array instead of a stdClass object. json_decode returns null on parse failure; always check json_last_error() === JSON_ERROR_NONE after decoding. In PHP 7.3 and later you can pass the JSON_THROW_ON_ERROR flag to throw a JsonException instead of returning null on error.

How are JSON types mapped to PHP?

JSON string → single-quoted PHP string, JSON integer → PHP int literal, JSON float → PHP float literal, JSON boolean → PHP true or false, JSON null → PHP null, JSON array → PHP array literal with numeric keys, JSON object → PHP associative array with string keys.

What is the difference between json_decode with true vs false?

Passing true as the second argument to json_decode returns PHP associative arrays for JSON objects — this is recommended for most use cases because array access is familiar and works with array functions. Passing false (the default) returns stdClass objects where properties are accessed with the arrow operator (->property). Associative arrays are generally easier to work with in PHP and are compatible with more standard library functions.

How do I re-encode a PHP array back to JSON?

Use json_encode($array) to convert a PHP array back to a JSON string. Add the JSON_PRETTY_PRINT flag for human-readable output: json_encode($array, JSON_PRETTY_PRINT). Use JSON_UNESCAPED_UNICODE to prevent Unicode characters from being escaped to \uXXXX sequences. Combine flags with the bitwise OR operator: json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE).

How do I handle JSON decode errors in PHP?

After calling json_decode, check json_last_error() === JSON_ERROR_NONE to verify success. In PHP 7.3 and later, pass JSON_THROW_ON_ERROR as the third argument to get a JsonException thrown on failure instead of returning null — this integrates cleanly with try/catch error handling. json_last_error_msg() returns a human-readable error description for logging.

Can I use this with Laravel?

Yes. Laravel's Request::json() method and response()->json($array) both use json_decode and json_encode internally. The generated PHP array works directly as controller return data, config file values, or seed data. In Laravel, you can also use Arr::wrap() and collect() helpers on the resulting array for further manipulation.

How do I validate JSON structure in PHP?

PHP 8.3 introduced json_validate() for fast validation without decoding. For older PHP versions, use json_decode + json_last_error() check. For JSON Schema validation (validating structure and types against a schema), use the justinrainbow/json-schema Composer package. Laravel also provides validation rules like json and array for request validation in controllers.