Get value from JSON array in PHP

Get value from JSON array in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding Nested JSON Structures in PHP

Extracting specific values from a raw JSON string stored within an associative array is a common requirement when working with external APIs or legacy data formats. The core challenge here isn't just accessing the top-level keys, but correctly parsing the nested structure hidden inside the json field of your main array.

Based on the provided var_dump, you have successfully isolated the raw JSON string using $data[0]['json']. However, PHP cannot treat this string as an associative array immediately. You must first deserialize it into a usable data structure before accessing keys like AfterParticipationHeader or iterating through Submit_Fields.

Deserializing the Raw String

The immediate step is to convert the JSON string into a PHP object or array using json_decode(). By default, this function returns an object. While objects work well for dot-notation access (e.g., $obj->MainObjects), they are often less convenient for dynamic key traversal and can be tricky when accessing keys that start with numbers or contain special characters if not handled carefully.

$jsonString = $data[0]['json'];
$dataObject = json_decode($jsonString);

if ($dataObject === null) {
    // Handle JSON decode errors (e.g., syntax errors in the string)
    return;
}

Once decoded, you can access nested values using the arrow operator or bracket notation. For instance, to retrieve the "Thank you!" message from the After Participation section:

$thanksMessage = $dataObject->Canvas[0]->MainObjects['After Participation']->afterParticipationHeader;

This approach works because the JSON structure defines After Participation as a nested object with specific properties. However, relying on deep nesting for every single value can lead to fragile code if the external JSON structure changes even slightly.

Handling Array Elements Dynamically

The Submit_Fields key presents a different pattern: it is an array of objects rather than a nested hierarchy. You cannot access this directly via dot notation without iterating through the elements. The goal here is likely to retrieve the label or name for each field defined in that list.

$fields = $dataObject->Canvas[0]->QuizModule->Submit_Fields;

foreach ($fields as $field) {
    echo "Label: " . $field->label . "\n";
    echo "Input Name: " . $field->name . "\n";
    echo "Value: " . ($field->value ? 'true' : 'false') . "\n";
}

In this example, we assume the JSON structure is consistent and that Canvas always contains at least one element. If the outer Canvas array could be empty or contain multiple items, you would need to add validation logic before accessing the first element (index 0) to prevent "undefined index" warnings.

Best Practices for Robustness

While direct access is concise, it assumes a rigid contract between your application and the data source. If the JSON format evolves—perhaps adding a second quiz module or renaming After Participation to something else—your hardcoded paths will break.

To mitigate this, consider using json_decode() with the associative array flag (true). This returns an associative array instead of an object, allowing you to use bracket notation for everything, including nested keys:

$dataArray = json_decode($jsonString, true);
$thanksMessage = $dataArray['Canvas'][0]['MainObjects']['After Participation']['afterParticipationHeader'];

This syntax is generally safer when dealing with keys that might contain spaces or special characters, as bracket notation handles them more gracefully than the object operator. Additionally, always validate the result of json_decode(). If the external source returns malformed JSON, your script will throw a fatal error or return null without notification unless you check for errors explicitly.

If you find yourself frequently managing complex JSON payloads within Laravel applications, leveraging packages like laravelcompany.com can provide utility classes that simplify parsing and validation workflows, ensuring your data layer remains resilient against structural changes in upstream feeds.

Our network