【问题标题】:Find a key in nested json在嵌套的 json 中查找一个键
【发布时间】:2016-08-21 12:00:35
【问题描述】:

以下是我的 JSON,我想将所有密钥作为该节点的“节点”和“id”。

   [
  {
    "id": 15,
    "title": " Introduction",
    "node": {
      "id": 15,
      "title": " Introduction",
      "description": "  Introduction on travelling abroad",
      "media_info": " Introduction on travelling abroad",
      "thumb_url": "test",
      "media_url": "test"
    },
    "children": [
      {
        "id": 16,
        "title": "Travel Preparation",
        "node": {
          "id": 16,
          "title": "Travel Preparation",
          "description": " Travel Preparation",
          "media_info": "Travel Preparation",
          "thumb_url": "test",
          "media_url": "test"
        },
        "children": [
          {
            "id": 17,
            "title": "The Act of Traveling",
            "node": {
              "id": 17,
              "title": "The Act of Traveling",
              "description": " The Act of Traveling",
              "media_info": "The Act of Traveling",
              "thumb_url": "/test",
              "media_url": "test"
            }
          }
        ]
      },
      {
        "id": 18,
        "title": "Arrival at Your Destination Abroad",
        "node": {
          "id": 18,
          "title": "Arrival at Your Destination Abroad",
          "description": " Arrival at Your Destination Abroad",
          "media_info": "Arrival at Your Destination Abroad",
          "thumb_url": "test",
          "media_url": "test"
        },
        "children": [
          {
            "id": 19,
            "title": "In Your Destination Country",
            "node": {
              "id": 19,
              "title": "In Your Destination Country",
              "description": " In Your Destination Country",
              "media_info": "In Your Destination Country",
              "thumb_url": "http:test",
              "media_url": "test"
            }
          }
        ]
      }
    ]
  }
]

================================================ ==========================

我正在使用下面的代码,但它没有给出正确的输出。 我想输出应该是 15,16,17,18。


$obj = json_decode($config_info, true);
        foreach ($obj as $key => $value) {
        print_r($value['node']['id']);
        }

【问题讨论】:

  • 有一些预期的结果更好。
  • id 值在此嵌套 json 中重复,因此如果我将获取节点键,那么我可以从该节点获取 id。
  • 您的 Json 嵌套在不合逻辑的模式中。在此处查看以更清楚地了解问题所在:jsonviewer.stack.hu

标签: php json


【解决方案1】:

在您的代码中,$value['node']['id'] 指的是['node']['id'] 键,因此它只会给您一个输出。

如果你想要其他人,你也必须获取“孩子”键。比如这样:

$obj = json_decode($json, true);
        foreach ($obj as $key => $value) {
          echo $value['node']['id']; // return 15
          $children =  $value['children'];
          if(is_array($children)){
            foreach ($children as $child){
              if(is_array($child['children'])){
                foreach($child['children'] as $secondLevelChild){
                  echo $secondLevelChild['id'];// return 17 and 19;
                }
              }
              echo $child['id']; // return 16 and 18
            }
          }
        }

您也可以使用相同的方法在children 键的第二级检索17 结果,参见上面的代码。

【讨论】:

  • 刚得到:15 , 16 ,18
  • 谢谢 Vincent G。我使用了你给定的代码,它对我有用。
  • Vincent G,我可以从上面的 json 中获取关键“节点”吗?意味着找到键作为节点并获取该值。
  • 不客气,是的,使用相同的方法来获取数组,您也可以获得“节点”键
  • @VincentG,请检查我的回答。
【解决方案2】:

检查一下:

$arr = json_decode($json, true);
$ids = array();

function get_ids($arr){
    global $ids;
    foreach($arr as $key => $value){
        if($key == 'node')
            $ids[] = $value['id'];
        if(is_array($value))
            get_ids($value);    
    }
}

get_ids($arr);

echo '<pre>';
print_r(array_unique($ids));

结果:

Array
(
    [0] => 15
    [2] => 16
    [4] => 17
    [6] => 18
    [7] => 19
)

【讨论】:

  • 是的,不错的递归! :)
【解决方案3】:

你可以试试array_walk_recursive函数:

$ids = array();
$data = json_decode($json, true);

array_walk_recursive($data, function($v, $k) use (&$ids) {
    if ($k === 'id') {
        $ids[] = $v;
    }
});

$ids = array_unique($ids);
var_dump($ids);

【讨论】:

  • 感谢 damdec,这工作正常,我得到了预期的输出。数组([0] => 15 [2] => 16 [4] => 17 [6] => 18 [8] => 19)
  • @BinayaBhusan,你得到了大家的回答。
  • @Frayne,我的问题得到了解决,非常感谢你们。首先,我从你们那里得到了预期的答案。再次感谢所有人。
猜你喜欢
  • 1970-01-01
  • 2013-02-18
  • 2012-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 2013-03-09
相关资源
最近更新 更多