在嵌套的 json 中查找键


Find a key in nested json

下面是我的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']);
        }
你可以

尝试使用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);

在你的代码中,$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结果,请参阅上面的代码。

看看:

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