-4

This is my code , how can I achieve it plz help

<?php

$json = '
{
    "type": "donut",
    "name": "Cake",
    "toppings": [{"data":
        { "id": "5002", "type": "Glazed" },
        { "id": "5006", "type": "Chocolate with Sprinkles" },
        { "id": "5004", "type": "Maple" }
    }]
}';

$yummy = json_decode($json);

echo $yummy->toppings[0]->data[2]->id; 
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Lol
  • 1
  • 1

1 Answers1

0

Fix the JSON, you're missing brackets. Once you fix this your code will work (it returns 5004 in your example)

{
    "type": "donut",
    "name": "Cake",
    "toppings": [{"data":
            [{ "id": "5002", "type": "Glazed" },
            { "id": "5006", "type": "Chocolate with Sprinkles" },
            { "id": "5004", "type": "Maple" }]
        }]
}

There are online tools that help you find these little issues.

https://jsonformatter.curiousconcept.com/

kissumisha
  • 484
  • 4
  • 12