PHP:嵌套的 JSON 和值提取

3

我有以下的json。我试图使用json_decode获取值。我得到了一些值,但是对于深层嵌套的值我遇到了麻烦。以下是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'];
}

然而,有没有一种简单的方法来获取bob、john、jake和frank的所有“emailAddresses”呢?
谢谢!
3个回答

2

最简单的方法就是循环,但首先在$decoded_array['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,则稍微高级一些的方法是使用array_walk_recursive()遍历数组查找该键。这将收集所有名为emailAddress的键,而不仅仅是在['people']子数组中的键。请注意保留HTML标签。
$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;
  }
});

0

脚本:

$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
    }
}

输出:

bob@gmail.com
john@gmail.com
jake@gmail.com
frank@gmail.com


实时示例:

http://codepad.org/R1K0Lysi


0

尝试:

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';
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接