简体   繁体   中英

PHP getting values from nested 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?

You need to delve deeper into the array.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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