drbl91357 2014-04-03 02:04
浏览 59
已采纳

PHP从嵌套的json中获取值

I have this json listed below. I was using json_decode to get some of the values. Such as getting the id value:

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

This method is working for getting the id value, however, I want to get the emailAddress values for both Bob and John. I believe you can get a single value by doing this:

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

Is it possible to get both email addresses in an efficient manner?

Edited --------

How would you get data with an expanded dataset? Example:

{
"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"
            }
        }
    }
]

}

I only want to extract the email addresses from both "fields". Is there an easy way to loop through all the "fields" to get "emailAddress" data?

  • 写回答

1条回答 默认 最新

  • douyou7878 2014-04-03 02:07
    关注

    You need to delve deeper into the array.

    foreach ($decoded_array['issue'][0]['fields']['people'] as $person) {
      echo $person['emailAddress'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!