mercredi 2 août 2017

Accessing Nested JSON Data Look for All Matching Key(s)

I have a variable that contain this json object

{
  "mac": "10:20:30:40:50:81",
  "type": "HGW",
  "children": [
    {
      "mac": "98:D6:D6:D8:FF:34",
      "pendingMethods": false,
      "lastSeen": "2017-05-24T10:36:35",
      "lastSeenLight": "GREEN",
      "model": "AP7465CE-TN",
      "type": "WIRELESS_ACCESS_POINT"
    },
    {
      "mac": "44:66:E9:A1:2C:DC",
      "pendingMethods": false,
      "lastSeen": "2017-05-24T10:39:01",
      "lastSeenLight": "GREEN",
      "model": "PLC 200+ DIV -TN",
      "type": "POWERLINE",
      "children": [
        {
          "mac": "22:50:9B:AB:2C:E6",
          "pendingMethods": false,
          "lastSeen": "2017-05-24T10:32:59",
          "lastSeenLight": "GREEN",
          "model": "PLC 200+ DIV -TN",
          "type": "POWERLINE",
          "children": [
            {
              "mac": "D1:D1:95:B6:67:21",
              "pendingMethods": false,
              "lastSeen": "2017-05-24T10:36:59",
              "lastSeenLight": "GREEN",
              "model": "AP7465CE-TN",
              "type": "WIRELESS_ACCESS_POINT"
            }
          ]
        }
      ]
    },
    {
      "mac": "D8:C2:A9:1C:44:47",
      "pendingMethods": "False",
      "lastSeen": "2017-05-24T10:39:01",
      "lastSeenLight": "GREEN",
      "model": "PG9073",
      "type": "POWERLINE",
      "children": [
        {
          "mac": "22:CD:E6:8F:8C:B8",
          "pendingMethods": false,
          "lastSeen": "2017-05-24T10:38:16",
          "lastSeenLight": "GREEN",
          "model": "PG9073",
          "type": "POWERLINE",
          "children": [
            {
              "mac": "64:CE:D0:0D:E9:CF",
              "pendingMethods": false,
              "lastSeen": "2017-05-24T10:26:56",
              "lastSeenLight": "GREEN",
              "model": "WNDR4300TN",
              "type": "WIRELESS_ACCESS_POINT"
            }
          ]
        },
        {
          "mac": "13:E4:AB:33:36:AC",
          "pendingMethods": false,
          "lastSeen": "2017-05-24T10:29:13",
          "lastSeenLight": "GREEN",
          "model": "PG9072",
          "type": "POWERLINE_WIRELESS_ACCESS_POINT"
        }
      ]
    }
  ]
}


I'm trying to loop through this json and access all the sub children property, and construct an array out of the matching one. If I count it manually I should get 8 match sub children.
This is not a straigtforward because I don't know which block of my JSON data contain the key children, I have to keep checking base on layer, but please feel free to give me any suggestions for improvement.

I've tried

if ($vcpe_hgw_mac != null) {

  $aps = $vcpe_hgw_mac['children'];

  $access_devices = [];
  $uuid = VSE::vcpe_ext($cpe_mac)['data']['account_uuid'];
  $ap_limits = VSE::licensedAPsLimit($uuid)['data']['ap_limit']; //2

  foreach ($aps as $i => $ap) {

        $vcpe_hgw_mac = str_replace(':', '', $ap['mac']);
        $access_device = VSE::accessdevices($vcpe_hgw_mac)['data'][0];

        if ($access_device != null) {

            $access_devices[$i]['channel2'] = $access_device['wireless'][0]['channel'];
            $access_devices[$i]['channel5'] = $access_device['wireless'][1]['channel'];
            $access_devices[$i]['cpe_mac'] = $vcpe_hgw_mac;
        }

        //Second Layer
        if(isset($ap['children'])){

            $second_layer_children = $ap['children'];

            foreach ($second_layer_children as $second_layer_child) {

                $vcpe_hgw_mac = str_replace(':', '', $second_layer_child['mac']);
                $access_device = VSE::accessdevices($vcpe_hgw_mac)['data'][0];

                if ($access_device != null) {
                    $i++;
                    $access_devices[$i]['channel2'] = $access_device['wireless'][0]['channel'];
                    $access_devices[$i]['channel5'] = $access_device['wireless'][1]['channel'];
                    $access_devices[$i]['cpe_mac']      = $vcpe_hgw_mac;
                }

                //Third layer
                if(isset($second_layer_child['children'])){

                    $third_layer_children = $second_layer_child['children'];

                    foreach ($third_layer_children as $third_layer_child) {

                        $vcpe_hgw_mac = str_replace(':', '', $third_layer_child['mac']);
                        $access_device = VSE::accessdevices($vcpe_hgw_mac)['data'][0];

                        if ($access_device != null) {
                            $i++;
                            $access_devices[$i]['channel2'] = $access_device['wireless'][0]['channel'];
                            $access_devices[$i]['channel5'] = $access_device['wireless'][1]['channel'];
                            $access_devices[$i]['cpe_mac']      = $vcpe_hgw_mac;

                        }

                        //Fourth Layer
                        if(isset($third_layer_child['children'])){

                            $fourth_layer_children = $third_layer_child['children'];

                            foreach ($fourth_layer_children as $fourth_layer_child) {

                                $vcpe_hgw_mac = str_replace(':', '', $fourth_layer_child['mac']);
                                $access_device = VSE::accessdevices($vcpe_hgw_mac)['data'][0];

                                if ($access_device != null) {
                                    $i++;
                                    $access_devices[$i]['channel2'] = $access_device['wireless'][0]['channel'];
                                    $access_devices[$i]['channel5'] = $access_device['wireless'][1]['channel'];
                                    $access_devices[$i]['cpe_mac']      = $vcpe_hgw_mac;

                                }
                            }
                        }

                    }
                }
            }
        }

    }
}

dd($access_devices);

accordingly to the reponse of my json I should to get back 8 children.

But I only get 6.

I'm stuck now. Can someone please give me a push ? What did I do wrong in my loop ?

Any hints / suggestions on this will be much appreciated !



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire