簡體   English   中英

PHP從嵌套的json中獲取值

[英]PHP getting values from nested json

我在下面列出了這個json。 我使用json_decode來獲取一些值。 比如獲取id值:

$decoded_array = json_decode($result, true);
            foreach($decoded_array['issue'] as $issues ){
                    $value[] = $issues["id"];

此方法用於獲取id值,但是,我想獲取Bob和John的emailAddress值。 我相信你可以通過這樣做得到一個單一的價值:

$value[] = $issues["fields"][people][0][emailAddress];

是否有可能以有效的方式獲得兩個電子郵件地址?

編輯--------

您如何獲得擴展數據集的數據? 例:

{
"startAt": 0,
"issue": [
    {
        "id": "51526",
        "fields": {
            "people": [
                {
                    "name": "bob",
                    "emailAddress": "bob@gmail.com",
                    "displayName": "Bob Smith",
                },
                {
                    "name": "john",
                    "emailAddress": "john@gmail.com",
                    "displayName": "John Smith",
                }
            ],
            "skill": {
                "name": "artist",
                "id": "1"
            }
        }
    },
{
        "id": "2005",
        "fields": {
            "people": [
                {
                    "name": "jake",
                    "emailAddress": "jake@gmail.com",
                    "displayName": "Jake Smith",
                },
                {
                    "name": "frank",
                    "emailAddress": "frank@gmail.com",
                    "displayName": "Frank Smith",
                }
            ],
            "skill": {
                "name": "writer",
                "id": "2"
            }
        }
    }
]

}

我只想從兩個“字段”中提取電子郵件地址。 是否有一種簡單的方法可以遍歷所有“字段”以獲取“emailAddress”數據?

您需要深入研究數組。

foreach ($decoded_array['issue'][0]['fields']['people'] as $person) {
  echo $person['emailAddress'];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM