PHP associative array pass keys as string

25 views Asked by At

This works fine. But how can I ref ['toppings'][2]['type'] by passing it as a string? i.e. $cake = '['toppings'][2]['type']'; echo $yummy + $cake;

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

    $yummy = json_decode($json, true);

    echo $yummy['toppings'][2]['type']; //Maple
?>

Thanks

0

There are 0 answers