从嵌套的JSON数组中打印特定数据

时间:2018-01-26 14:17:29

标签: php arrays json

您好,我想打印枫树的确切数据,但我不工作,请帮助我 有些问题我无法识别

$json = ' {
"Success":true,"Message":null,"Data":
{
    "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

2 个答案:

答案 0 :(得分:5)

你错过['Data']

$yummy['Data']['toppings'][2]['type']; //Maple `

答案 1 :(得分:-1)

print_r($yummy, true);

为json提供数组数据。

Array
(
    [Success] => 1
    [Message] => 
    [Data] => Array
        (
            [type] => donut
            [name] => Cake
            [toppings] => Array
                (
                    [0] => Array
                        (
                            [id] => 5002
                            [type] => Glazed
                        )

                    [1] => Array
                        (
                            [id] => 5006
                            [type] => Chocolate with Sprinkles
                        )

                    [2] => Array
                        (
                            [id] => 5004
                            [type] => Maple
                        )

                )

        )

)

所以你错过了数据

$yummy['Data']['toppings'][2]['type']