Binaya Bhusan
Binaya Bhusan

Reputation: 11

Find a key in nested json

Below is my JSON and i wan to get all key as "node"and "id" of that node.

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

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

I am using below code but it is not giving the correct output. I want out put should be 15,16,17,18.


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

Upvotes: 0

Views: 2488

Answers (3)

damdec
damdec

Reputation: 102

You could try with array_walk_recursive function :

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

Upvotes: 4

Murad Hasan
Murad Hasan

Reputation: 9583

Check it out:

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

Result:

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

Upvotes: 1

Nutshell
Nutshell

Reputation: 8537

In your code, $value['node']['id'] refer to the ['node']['id'] key, so it will give you only one output.

In you want others, you have to fetch "children" key as well. Like this for instance :

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

And you can use the same method to retrieve the 17 result as well in the second level of children key, see the code above.

Upvotes: 1

Related Questions