PHP从嵌套的json中获取值

时间:2014-04-03 02:04:38

标签: php 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”数据?

1 个答案:

答案 0 :(得分:2)

您需要深入研究数组。

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