Decode a JSON string into a PHP value.
The json_decode() function decodes a JSON-encoded string and returns the corresponding PHP value. By default, JSON objects are returned as stdClass objects.
Setting the associative parameter to true causes JSON objects to be returned as associative arrays instead. This is often preferred for easier array access syntax.
The depth parameter sets the maximum nesting depth for decoding. The flags parameter accepts options like JSON_THROW_ON_ERROR and JSON_BIGINT_AS_STRING. On failure, the function returns null, and json_last_error() can be used to determine the cause.
| Name | Description | Optional |
|---|---|---|
json |
The JSON string to decode. | No |
associative |
When true, JSON objects are returned as associative arrays. | Yes |
depth |
Maximum nesting depth of the structure being decoded. | Yes |
flags |
Bitmask of JSON decoding options. | Yes |
$json = '{"name":"Alice","age":30}';
$data = json_decode($json, true);
echo $data['name']; // 'Alice'