Master PHP’s json_decode(): Parameters, Options, and Real‑World Examples

Learn how PHP’s json_decode() function converts JSON strings into objects or associative arrays, explore its required and optional parameters—including depth, associative flag, and decoding options—and see a complete code example that demonstrates extracting names, ages, and skill lists from decoded data.

php Courses
php Courses
php Courses
Master PHP’s json_decode(): Parameters, Options, and Real‑World Examples

json_decode() Overview

The json_decode() function converts a JSON‑encoded string into a native PHP value—either an object or an associative array—so that JSON data received from APIs, client‑side scripts, or configuration files can be accessed and manipulated in PHP.

Parameters

string $json

: The JSON string to be decoded. It must be valid JSON; otherwise an error is generated. bool $associative (optional, default false): When true, the function returns an associative array; when false, it returns an object of type stdClass. int $depth (optional, default 512): Maximum recursion depth. If the JSON structure exceeds this depth, json_decode() fails with an error. int $options (optional, default 0): Bitmask of decoding options. Common flags include: JSON_BIGINT_AS_STRING – converts large integers to strings to avoid overflow. JSON_OBJECT_AS_ARRAY – forces objects to be returned as associative arrays (similar to setting $associative to true). JSON_THROW_ON_ERROR – makes the function throw a JsonException on failure instead of returning null.

Code Example

The following script demonstrates decoding a JSON string into an associative array, accessing individual fields, and iterating over a nested array.

<?php
// JSON string
$json_data = '{
    "name": "Tom",
    "age": 25,
    "skills": ["PHP", "JavaScript", "HTML", "CSS"]
}';

// Decode to associative array
$array_data = json_decode($json_data, true);

// Output name and age
echo "Name: " . $array_data['name'] . "
";
echo "Age: " . $array_data['age'] . "
";

echo "Skills:
";
foreach ($array_data['skills'] as $skill) {
    echo "- " . $skill . "
";
}
?>

Steps performed by the script:

Define a JSON string containing a simple object with three properties.

Call json_decode($json_data, true) to obtain an associative array. The second argument true forces array output.

Access scalar values directly via their keys ( name and age).

Iterate over the skills array with a foreach loop to print each skill on a separate line.

Using json_decode() is essential for handling JSON in PHP because it provides a reliable way to transform external JSON payloads into native data structures, enabling further processing, validation, or storage.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPData Parsingjson_decode
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.