簡體   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