2017-11-11 58 views
0

我有一个很大的多维JSON数组,我试图用PHP解析(由于约束需要PHP)。我有一个变量中的下面,然后我在这个字符串上做一个高级多维PHP JSON解析问题

json_decode。我已经尝试过,但没有真正保存为数组。

$ json = json_decode($ result,true);

然后我尝试和借鉴,甚至一些简单的像

$ JSON [ '数据'] [ 'cfgversion'],它不工作。我也试过没有真正使用$ json->数据 - > cfgversion

我所想要知道的就是

“port_overrides”:[{ “port_idx”:6, “portconf_id “: “59d18fe9b6081ed3f75d3355” },{ “AUTONEG”:真, “隔离”:假, “名称”: “端口8”, “op_mode”: “开关”, “poe_mode”: “直通” , “port_idx”:8, “portconf_id”:“575797d9429272985a1db175”, “stormctrl_bcast_enabled”:假, “stormctrl_mcast_enabled”:假的, “stormctrl_ucast_enabled”:假 }],

我想修改像portconf_id件事数组中,然后将其推回(这是一个宇辉开关)

任何想法都会很棒。我看了其他JSOn PHP解析答案,他们似乎不适用于我的下面。提前致谢。

{ 
"data": [{ 
    "_id": "599cab6084aa6b25d7e356cd", 
    "_uptime": 1857874, 
    "adopted": true, 
    "board_rev": 10, 
    "bytes": 1057778283548, 
    "cfgversion": "9ef05412066832e5", 
    "config_network": { 
     "dns1": "10.0.1.1", 
     "dns2": "8.8.8.8", 
     "gateway": "10.0.1.1", 
     "ip": "10.0.1.15", 
     "netmask": "255.255.255.0", 
     "type": "static" 
    }, 
    "connect_request_ip": "10.0.1.15", 
    "connect_request_port": "42723", 
    "considered_lost_at": 1510378663, 
    "device_id": "599cab6084aa6b25d7e356cd", 
    "dhcp_server_table": [], 
    "dot1x_portctrl_enabled": false, 
    "downlink_table": [{ 
     "full_duplex": true, 
     "mac": "80:2a:a8:53:77:4b", 
     "port_idx": 8, 
     "speed": 1000 
    }], 
    "ethernet_table": [{ 
     "mac": "f0:9f:c2:1e:3c:d1", 
     "name": "eth0", 
     "num_port": 8 
    }, { 
     "mac": "f0:9f:c2:1e:3c:d2", 
     "name": "srv0" 
    }], 
    "flowctrl_enabled": false, 
    "fw_caps": 7685, 
    "general_temperature": 0, 
    "guest-num_sta": 0, 
    "has_fan": false, 
    "inform_authkey": "543fcde7cd54091e622a93686d1b3ecb", 
    "inform_ip": "10.0.1.52", 
    "inform_url": "http://10.0.1.52:8080/inform", 
    "ip": "10.0.1.15", 
    "jumboframe_enabled": false, 
    "known_cfgversion": "9ef05412066832e5", 
    "last_seen": 1510378492, 
    "led_override": "default", 
    "license_state": "registered", 
    "locating": false, 
    "mac": "f0:9f:c2:1e:3c:d1", 
    "model": "US8", 
    "name": "TV Room Switch", 
    "next_heartbeat_at": 1510378549, 
    "num_sta": 6, 
    "overheating": false, 
    "port_overrides": [{ 
     "port_idx": 6, 
     "portconf_id": "59d18fe9b6081ed3f75d3355" 
    }, { 
     "autoneg": true, 
     "isolation": false, 
     "name": "Port 8", 
     "op_mode": "switch", 
     "poe_mode": "passthrough", 
     "port_idx": 8, 
     "portconf_id": "575797d9429272985a1db175", 
     "stormctrl_bcast_enabled": false, 
     "stormctrl_mcast_enabled": false, 
     "stormctrl_ucast_enabled": false 
    }], 
    "port_table": [{ 
     "aggregated_by": false, 
     "autoneg": true, 
     "bytes-r": 11439, 
     "dot1x_mode": "auto", 
     "dot1x_status": "authorized", 
     "enable": true, 
     "flowctrl_rx": false, 
     "flowctrl_tx": false, 
     "full_duplex": true, 
     "is_uplink": true, 
     "jumbo": false, 
     "lldp_table": [{ 
      "lldp_chassis_id": "80:2A:A8:5D:92:A4", 
      "lldp_port_id": "0/15", 
      "lldp_system_name": "MainSwitch" 
     }], 
     "masked": false, 
     "media": "GE", 
     "name": "Port 1", 
     "op_mode": "switch", 
     "poe_caps": 0, 
     "port_idx": 1, 
     "port_poe": false, 
     "portconf_id": "575797d9429272985a1db175", 
     "rx_broadcast": 5988397, 
     "rx_bytes": 396387817490, 
     "rx_bytes-r": 2372, 
     "rx_dropped": 92655, 
     "rx_errors": 0, 
     "rx_multicast": 8148687, 
     "rx_packets": 606001308, 
     "speed": 1000, 
     "stp_pathcost": 0, 
     "stp_state": "forwarding", 
     "tx_broadcast": 1142032, 
     "tx_bytes": 661390466058, 
     "tx_bytes-r": 9066, 
     "tx_dropped": 0, 
     "tx_errors": 0, 
     "tx_multicast": 871097, 
     "tx_packets": 676585183, 
     "up": true 
    }, { 
     "aggregated_by": false, 
     "autoneg": true, 
     "bytes-r": 2415, 
     "dot1x_mode": "auto", 
     "dot1x_status": "authorized", 
     "enable": true, 
     "flowctrl_rx": false, 
     "flowctrl_tx": false, 
     "full_duplex": true, 
     "is_uplink": false, 
     "jumbo": false, 
     "lldp_table": [], 
     "masked": false, 
     "media": "GE", 
     "name": "Port 2", 
     "op_mode": "switch", 
     "poe_caps": 0, 
     "port_idx": 2, 
     "port_poe": false, 
     "portconf_id": "575797d9429272985a1db175", 
     "rx_broadcast": 72457, 
     "rx_bytes": 3344218780, 
     "rx_bytes-r": 507, 
     "rx_dropped": 3542, 
     "rx_errors": 0, 
     "rx_multicast": 144549, 
     "rx_packets": 36641209, 
     "speed": 1000, 
     "stp_pathcost": 0, 
     "stp_state": "forwarding", 
     "tx_broadcast": 7058001, 
     "tx_bytes": 160950919257, 
     "tx_bytes-r": 1907, 
     "tx_dropped": 0, 
     "tx_errors": 0, 
     "tx_multicast": 8813991, 
     "tx_packets": 125070289, 
     "up": true 
    }, { 
     "aggregated_by": false, 
     "autoneg": true, 
     "bytes-r": 10396, 
     "dot1x_mode": "auto", 
     "dot1x_status": "authorized", 
     "enable": true, 
     "flowctrl_rx": false, 
     "flowctrl_tx": false, 
     "full_duplex": true, 
     "is_uplink": false, 
     "jumbo": false, 
     "lldp_table": [], 
     "masked": false, 
     "media": "GE", 
     "name": "Port 3", 
     "op_mode": "switch", 
     "poe_caps": 0, 
     "port_idx": 3, 
     "port_poe": false, 
     "portconf_id": "575797d9429272985a1db175", 
     "rx_broadcast": 77, 
     "rx_bytes": 3384400206, 
     "rx_bytes-r": 8063, 
     "rx_dropped": 0, 
     "rx_errors": 0, 
     "rx_multicast": 4150, 
     "rx_packets": 6039069, 
     "speed": 100, 
     "stp_pathcost": 0, 
     "stp_state": "forwarding", 
     "tx_broadcast": 7130309, 
     "tx_bytes": 3249279910, 
     "tx_bytes-r": 2332, 
     "tx_dropped": 0, 
     "tx_errors": 0, 
     "tx_multicast": 8954291, 
     "tx_packets": 19408520, 
     "up": true 
    }, { 
     "aggregated_by": false, 
     "autoneg": true, 
     "bytes-r": 0, 
     "dot1x_mode": "n/a", 
     "dot1x_status": "n/a", 
     "enable": true, 
     "flowctrl_rx": false, 
     "flowctrl_tx": false, 
     "full_duplex": false, 
     "is_uplink": false, 
     "jumbo": false, 
     "lldp_table": [], 
     "masked": false, 
     "media": "GE", 
     "name": "Port 4", 
     "op_mode": "switch", 
     "poe_caps": 0, 
     "port_idx": 4, 
     "port_poe": false, 
     "portconf_id": "575797d9429272985a1db175", 
     "rx_broadcast": 0, 
     "rx_bytes": 0, 
     "rx_bytes-r": 0, 
     "rx_dropped": 0, 
     "rx_errors": 0, 
     "rx_multicast": 0, 
     "rx_packets": 0, 
     "speed": 0, 
     "stp_pathcost": 0, 
     "stp_state": "disabled", 
     "tx_broadcast": 0, 
     "tx_bytes": 0, 
     "tx_bytes-r": 0, 
     "tx_dropped": 0, 
     "tx_errors": 0, 
     "tx_multicast": 0, 
     "tx_packets": 0, 
     "up": false 
    }, { 
     "aggregated_by": false, 
     "autoneg": true, 
     "bytes-r": 0, 
     "dot1x_mode": "n/a", 
     "dot1x_status": "n/a", 
     "enable": true, 
     "flowctrl_rx": false, 
     "flowctrl_tx": false, 
     "full_duplex": false, 
     "is_uplink": false, 
     "jumbo": false, 
     "lldp_table": [], 
     "masked": false, 
     "media": "GE", 
     "name": "Port 5", 
     "op_mode": "switch", 
     "poe_caps": 0, 
     "port_idx": 5, 
     "port_poe": false, 
     "portconf_id": "575797d9429272985a1db175", 
     "rx_broadcast": 0, 
     "rx_bytes": 0, 
     "rx_bytes-r": 0, 
     "rx_dropped": 0, 
     "rx_errors": 0, 
     "rx_multicast": 0, 
     "rx_packets": 0, 
     "speed": 0, 
     "stp_pathcost": 0, 
     "stp_state": "disabled", 
     "tx_broadcast": 0, 
     "tx_bytes": 0, 
     "tx_bytes-r": 0, 
     "tx_dropped": 0, 
     "tx_errors": 0, 
     "tx_multicast": 0, 
     "tx_packets": 0, 
     "up": false 
    }, { 
     "aggregated_by": false, 
     "autoneg": true, 
     "bytes-r": 0, 
     "dot1x_mode": "n/a", 
     "dot1x_status": "n/a", 
     "enable": true, 
     "flowctrl_rx": false, 
     "flowctrl_tx": false, 
     "full_duplex": false, 
     "is_uplink": false, 
     "jumbo": false, 
     "lldp_table": [], 
     "masked": false, 
     "media": "GE", 
     "name": "Port 6", 
     "op_mode": "switch", 
     "poe_caps": 0, 
     "port_idx": 6, 
     "port_poe": false, 
     "portconf_id": "59d18fe9b6081ed3f75d3355", 
     "rx_broadcast": 0, 
     "rx_bytes": 0, 
     "rx_bytes-r": 0, 
     "rx_dropped": 0, 
     "rx_errors": 0, 
     "rx_multicast": 0, 
     "rx_packets": 0, 
     "speed": 0, 
     "stp_pathcost": 0, 
     "stp_state": "disabled", 
     "tx_broadcast": 0, 
     "tx_bytes": 0, 
     "tx_bytes-r": 0, 
     "tx_dropped": 0, 
     "tx_errors": 0, 
     "tx_multicast": 0, 
     "tx_packets": 0, 
     "up": false 
    }, { 
     "aggregated_by": false, 
     "autoneg": true, 
     "bytes-r": 0, 
     "dot1x_mode": "n/a", 
     "dot1x_status": "n/a", 
     "enable": true, 
     "flowctrl_rx": false, 
     "flowctrl_tx": false, 
     "full_duplex": false, 
     "is_uplink": false, 
     "jumbo": false, 
     "lldp_table": [], 
     "masked": false, 
     "media": "GE", 
     "name": "Port 7", 
     "op_mode": "switch", 
     "poe_caps": 0, 
     "port_idx": 7, 
     "port_poe": false, 
     "portconf_id": "575797d9429272985a1db175", 
     "rx_broadcast": 0, 
     "rx_bytes": 0, 
     "rx_bytes-r": 0, 
     "rx_dropped": 0, 
     "rx_errors": 0, 
     "rx_multicast": 0, 
     "rx_packets": 0, 
     "speed": 0, 
     "stp_pathcost": 0, 
     "stp_state": "disabled", 
     "tx_broadcast": 0, 
     "tx_bytes": 0, 
     "tx_bytes-r": 0, 
     "tx_dropped": 0, 
     "tx_errors": 0, 
     "tx_multicast": 0, 
     "tx_packets": 0, 
     "up": false 
    }, { 
     "aggregated_by": false, 
     "autoneg": true, 
     "bytes-r": 2226, 
     "dot1x_mode": "auto", 
     "dot1x_status": "authorized", 
     "enable": true, 
     "flowctrl_rx": false, 
     "flowctrl_tx": false, 
     "full_duplex": true, 
     "is_uplink": false, 
     "isolation": false, 
     "jumbo": false, 
     "lldp_table": [], 
     "masked": false, 
     "media": "GE", 
     "name": "Port 8", 
     "op_mode": "switch", 
     "poe_caps": 8, 
     "poe_enable": true, 
     "poe_mode": "passthrough", 
     "poe_voltage": "48.00", 
     "port_idx": 8, 
     "port_poe": true, 
     "portconf_id": "575797d9429272985a1db175", 
     "rx_broadcast": 1012063, 
     "rx_bytes": 655667898453, 
     "rx_bytes-r": 362, 
     "rx_dropped": 28656, 
     "rx_errors": 0, 
     "rx_multicast": 630304, 
     "rx_packets": 634872135, 
     "speed": 1000, 
     "stormctrl_bcast_enabled": false, 
     "stormctrl_mcast_enabled": false, 
     "stormctrl_ucast_enabled": false, 
     "stp_pathcost": 0, 
     "stp_state": "forwarding", 
     "tx_broadcast": 6116174, 
     "tx_bytes": 239145629938, 
     "tx_bytes-r": 1864, 
     "tx_dropped": 0, 
     "tx_errors": 0, 
     "tx_multicast": 8341073, 
     "tx_packets": 495246848, 
     "up": true 
    }], 
    "power_source": "poe", 
    "power_source_voltage": "48.00", 
    "rx_bytes": 661390466058, 
    "serial": "F09FC21E3CD1", 
    "site_id": "575797d7429272985a1db16c", 
    "ssh_session_table": [], 
    "stat": { 
     "bytes": 3.90939602E8, 
     "datetime": "2017-11-11T04:10:00Z", 
     "duration": 4950000.0, 
     "o": "sw", 
     "oid": "f0:9f:c2:1e:3c:d1", 
     "port_1-rx_bytes": 6.9195452E7, 
     "port_1-rx_dropped": 304.0, 
     "port_1-rx_packets": 181516.0, 
     "port_1-tx_bytes": 1.16929998E8, 
     "port_1-tx_packets": 168689.0, 
     "port_2-rx_bytes": 3303986.0, 
     "port_2-rx_dropped": 9.0, 
     "port_2-rx_packets": 11352.0, 
     "port_2-tx_bytes": 1.0588899E7, 
     "port_2-tx_packets": 56442.0, 
     "port_3-rx_bytes": 8936568.0, 
     "port_3-rx_packets": 15892.0, 
     "port_3-tx_bytes": 1.1060388E7, 
     "port_3-tx_packets": 61268.0, 
     "port_8-rx_bytes": 1.04055643E8, 
     "port_8-rx_dropped": 89.0, 
     "port_8-rx_packets": 140018.0, 
     "port_8-tx_bytes": 6.6868668E7, 
     "port_8-tx_packets": 164236.0, 
     "rx_bytes": 1.85491649E8, 
     "rx_crypts": 0.0, 
     "rx_dropped": 402.0, 
     "rx_errors": 0.0, 
     "rx_frags": 0.0, 
     "rx_packets": 348778.0, 
     "site_id": "575797d7429272985a1db16c", 
     "sw": "f0:9f:c2:1e:3c:d1", 
     "time": 1510373400000, 
     "tx_bytes": 2.05447953E8, 
     "tx_dropped": 0.0, 
     "tx_errors": 0.0, 
     "tx_packets": 450635.0, 
     "tx_retries": 0.0 
    }, 
    "state": 1, 
    "stp_priority": "32768", 
    "stp_version": "rstp", 
    "sys_stats": { 
     "loadavg_1": "1.26", 
     "loadavg_15": "1.41", 
     "loadavg_5": "1.31", 
     "mem_buffer": 0, 
     "mem_total": 262766592, 
     "mem_used": 161001472 
    }, 
    "tx_bytes": 396387817490, 
    "type": "usw", 
    "uplink": { 
     "full_duplex": true, 
     "ip": "10.0.1.15", 
     "mac": "f0:9f:c2:1e:3c:d1", 
     "max_speed": 1000, 
     "media": "GE", 
     "name": "eth0", 
     "netmask": "255.255.255.0", 
     "num_port": 8, 
     "port_idx": 1, 
     "rx_bytes": 396387817490, 
     "rx_bytes-r": 2372, 
     "rx_dropped": 92655, 
     "rx_errors": 0, 
     "rx_multicast": 0, 
     "rx_packets": 606001308, 
     "speed": 1000, 
     "tx_bytes": 661390466058, 
     "tx_bytes-r": 9066, 
     "tx_dropped": 0, 
     "tx_errors": 0, 
     "tx_packets": 676585183, 
     "type": "wire", 
     "up": true, 
     "uplink_mac": "80:2a:a8:5d:92:a4" 
    }, 
    "uplink_depth": 2, 
    "uptime": 1857874, 
    "user-num_sta": 6, 
    "version": "3.9.3.7537", 
    "x_authkey": "543fcde7cd54091e622a93686d1b3ecb", 
    "x_fingerprint": "65:06:32:75:3a:af:99:8a:92:14:0e:1d:6d:3f:6b:7b", 
    "x_has_ssh_hostkey": true, 
    "x_ssh_hostkey_fingerprint": "65:06:32:75:3a:af:99:8a:92:14:0e:1d:6d:3f:6b:7b" 
}], 
"meta": { 
    "rc": "ok" 
} 

}

+0

它的一个数组,你应该做'$ JSON [ '数据'] [0] [ 'cfgversion']' –

+0

是的,就是这样非常感谢,所以我可以弄清楚我想用PHP中的数组完成很多其他事情。 – Type11

+0

愚蠢的问题,但我如何选择你的答案作为这里的正确答案?我无法找到允许的位置 – Type11

回答

0

劳伦斯有正确答案