PHP:嵌套json和值提取

PHP:嵌套json和值提取,php,foreach,json,Php,Foreach,Json,我在下面有这个json。我正在尝试使用json_解码获取值。我得到了一些值,但我在使用深度嵌套的值时遇到了问题。以下是json: { "startAt": 0, "issue": [ { "id": "51526", "fields": { "people": [ { "name": "bob", "emailAddress": "bob@gmail.com",

我在下面有这个json。我正在尝试使用json_解码获取值。我得到了一些值,但我在使用深度嵌套的值时遇到了问题。以下是json:

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

}
我知道通过这样做我可以得到一个值:

foreach ($decoded_array['issue'][0]['fields']['people'] as $person) {
  echo $person['emailAddress'];
}
然而,有没有一种简单的方法来获取鲍勃、约翰、杰克和弗兰克的所有“电子邮件地址”


谢谢

最简单的方法就是循环,但是嵌套循环首先在
$decoded_数组['issue']
上,然后在
['people']
上进行内部循环。将地址收集到输出数组中

// Note: this assumes you called json_decode() with the second
// param TRUE to force an associative array..
// $decoded_array = json_decode($input_json, TRUE);

$addresses = array();
foreach ($decoded_array['issue'] as $i) {
  foreach ($i['fields']['people'] as $person) {
    // Append the address onto an output array
    $addresses[] = $person['emailAddress'];
  }
}
// De-dupe them if necessary
$addresses = array_unique($addresses);
print_r($addresses);

// Prints
Array
(
    [0] => bob@gmail.com
    [1] => john@gmail.com
    [2] => jake@gmail.com
    [3] => frank@gmail.com
)
如果您对结构没有把握,除了键名为
emailAddress
之外,还有一种稍微奇特的方法可以用来遍历数组以查找该键。这将收集名为
emailAddress
的所有密钥,而不仅仅是
['people']
子数组中的密钥

$addresses = array();
// Pass $addresses into the closure by reference so you can write to it
array_walk_recursive($decoded_array, function($value, $key) use (&$addresses) {
  // Find *all keys* called emailAddress
  if ($key == 'emailAddress') {
    $addresses[] = $value;
  }
});
尝试:

脚本:
$json=“……”//您的json文本。
$decoded_array=json_decode($json)->{'issue'}//解码Json字符串
foreach($解码为$issue的_数组){//获取所有问题
foreach($issue->{'fields'}->{'people'}作为$person){//Get all people
echo$person->{'emailAddress'}.
;//获取此人的电子邮件 } }
输出: bob@gmail.com
john@gmail.com
jake@gmail.com
frank@gmail.com

实例:

function people($peopleArray, $searchProperty, $forValueArray){
  $fva = array_map('strtolower', $forValuArray);
  foreach($peopleArray as $v){
    $sp = $v[$searchProperty]; $sl = strtolower($sp);
    if(in_array($sl, $fva)){
      $q[] = $sp;
    }
  }
  return isset($q) ? $q : 0;
}
if($ans = people($decoded_array, 'emailAddress', array('bob', 'john', 'jake', 'frank'))){
  print_r($ans);
}
else{
  echo 'no results found';
}
$json = "......"                                          //Your json text. 
$decoded_array = json_decode($json)->{'issue'};           //Decode Json string
foreach($decoded_array as $issue){                        //Get all Issues
    foreach($issue->{'fields'}->{'people'} as $person){   //Get all people
        echo $person->{'emailAddress'}."<br/>";           //Get person's email
    }
}