繁体   English   中英

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

[英]Print Particular Data From Nested JSON Array

嗨,我想打印枫树的确切数据,但我无法正常工作,请帮助我,我无法识别某些问题

$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

您错过了['Data']

$yummy['Data']['toppings'][2]['type']; //Maple `
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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM