PHP:嵌套 json 和值提取

Posted

技术标签:

【中文标题】PHP:嵌套 json 和值提取【英文标题】:PHP: Nested json and value extraction 【发布时间】:2014-05-16 02:02:17 【问题描述】:

我在下面有这个 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 的所有“电子邮件地址”?

谢谢!

【问题讨论】:

【参考方案1】:

最简单的方法实际上就是循环,但首先在$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_recurisve() 遍历数组以查找该键。这将收集名为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;
  
);

【讨论】:

【参考方案2】:

试试:

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

【讨论】:

【参考方案3】:

脚本:

$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

【讨论】:

以上是关于PHP:嵌套 json 和值提取的主要内容,如果未能解决你的问题,请参考以下文章

从嵌套的json循环php中提取数据

如何在 R 数据框中取消嵌套列表,同时保留键和值?

无法使用 Spark/Scala 从 JSON 嵌套键值对创建列和值

如何使用 angular2 ngFor 深度嵌套的 json 值

从 Python 中的嵌套 Json 中提取信息

使用 Python 提取嵌套列