html - Print data retrieved in JSON on a PHP -
i'm trying print table list of monitoring groups data retrieved rest api. results come in json. below json data:
{ "code": 0, "message": "success", "data": [ { "group_id": "169839000000116001", "display_name": "mti servers", "description": "", "monitors": [ ] }, { "group_id": "169839000000180001", "display_name": "prb servers", "description": "", "monitors": [ "169839000000179003", "169839000000176013", "169839000000175003", "169839000000176007" ] }, { "group_id": "169839000000046270", "display_name": "db servers", "description": "", "monitors": [ "169839000000051011", "169839000000047023", "169839000000078001" ] }, { "group_id": "169839000000025200", "display_name": "ext apps", "description": "external monitoring of applications", "monitors": [ "169839000000025274", "169839000000025377", "169839000000025359", "169839000000025369", "169839000000025385", "169839000000025226" ] }, { "group_id": "169839000000025109", "display_name": "ext services", "description": "external monitoring of services.", "monitors": [ "169839000000046165", "169839000000025256", "169839000000025168", "169839000000025202", "169839000000025189", "169839000000025217", "169839000000025265" ] }, { "group_id": "169839000000046015", "display_name": "zmb servers", "description": "", "monitors": [ "169839000000050017", "169839000000050025", "169839000000049001", "169839000000050001", "169839000000053019", "169839000000051003", "169839000000050009" ] }, { "group_id": "169839000000046282", "display_name": "nwk devices", "description": "", "monitors": [ "169839000000082009", "169839000000084077", "169839000000084001", "169839000000082229" ] }, { "group_id": "169839000000046013", "display_name": "vbr servers", "description": "", "monitors": [ "169839000000047007" ] }, { "group_id": "169839000000054197", "display_name": "lnx servers", "description": "", "monitors": [ "169839000000060483" ] }, { "group_id": "169839000000046020", "display_name": "vsp servers", "description": "", "monitors": [ "169839000000060177", "169839000000060170", "169839000000060088", "169839000000060095", "169839000000060102", "169839000000060109", "169839000000054102" ] }, { "group_id": "169839000000046058", "display_name": "wnd servers", "description": "", "monitors": [ "169839000000066001", "169839000000063119" ] }, { "group_id": "169839000000128001", "display_name": "tpt servers", "description": "", "monitors": [ "169839000000143041", "169839000000148017", "169839000000127035", "169839000000123003", "169839000000126011", "169839000000122011", "169839000000129001", "169839000000158028" ] } ] }
i want print group id, name, description , list of monitor ids belong group. i'm using json_decode cant seem read correct variables array.
here's code have far:
`<?php /** * connect site api , extract list of monitor groups */ // url fetch $url = "https://www.mymonitorsite.com/api/monitor_groups"; // initialize curl session $ch = curl_init($url); // option return result, rather true/false curl_setopt($ch, curlopt_returntransfer, true); // set custom headers $headers = array( 'authorization: authtoken 12345678901234567890123456789012', 'content-type: application/json;charset=utf-8', 'accept: application/json; version=2.0', ); // option set custom headers curl_setopt($ch, curlopt_httpheader, $headers); // perform request, , save content $result $mongrps_json = curl_exec($ch); // close curl resource, , free system resources! curl_close($ch); // decode json data php array $mongrps_array = json_decode($mongrps_json, true); ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"/> <title>monitor group test</title> </head> <body> <?php // list first monitor group here (test) echo "group id = " . $mongrps_array->data[0]->group_id . "<br>"; echo "group name = " . $mongrps_array->data[0]->display_name . <br>"; ?> </body> </html> `
here's var_dump of json_decode:
array(3) { ["code"]=> int(0) ["message"]=> string(7) "success" ["data"]=> array(12) { [0]=> array(4) { ["group_id"]=> string(18) "169839000000116001" ["display_name"]=> string(11) "mti servers" ["description"]=> string(0) "" ["monitors"]=> array(0) { } } [1]=> array(4) { ["group_id"]=> string(18) "169839000000180001" ["display_name"]=> string(11) "prb servers" ["description"]=> string(0) "" ["monitors"]=> array(4) { [0]=> string(18) "169839000000179003" [1]=> string(18) "169839000000176013" [2]=> string(18) "169839000000175003" [3]=> string(18) "169839000000176007" } } [2]=> array(4) { ["group_id"]=> string(18) "169839000000046270" ["display_name"]=> string(10) "db servers" ["description"]=> string(0) "" ["monitors"]=> array(3) { [0]=> string(18) "169839000000051011" [1]=> string(18) "169839000000047023" [2]=> string(18) "169839000000078001" } } [3]=> array(4) { ["group_id"]=> string(18) "169839000000025200" ["display_name"]=> string(8) "ext apps" ["description"]=> string(35) "external monitoring of applications" ["monitors"]=> array(6) { [0]=> string(18) "169839000000025274" [1]=> string(18) "169839000000025377" [2]=> string(18) "169839000000025359" [3]=> string(18) "169839000000025369" [4]=> string(18) "169839000000025385" [5]=> string(18) "169839000000025226" } } [4]=> array(4) { ["group_id"]=> string(18) "169839000000025109" ["display_name"]=> string(12) "ext services" ["description"]=> string(31) "external monitoring of services" ["monitors"]=> array(7) { [0]=> string(18) "169839000000046165" [1]=> string(18) "169839000000025256" [2]=> string(18) "169839000000025168" [3]=> string(18) "169839000000025202" [4]=> string(18) "169839000000025189" [5]=> string(18) "169839000000025217" [6]=> string(18) "169839000000025265" } } [5]=> array(4) { ["group_id"]=> string(18) "169839000000046015" ["display_name"]=> string(11) "zmb servers" ["description"]=> string(0) "" ["monitors"]=> array(7) { [0]=> string(18) "169839000000050017" [1]=> string(18) "169839000000050025" [2]=> string(18) "169839000000049001" [3]=> string(18) "169839000000050001" [4]=> string(18) "169839000000053019" [5]=> string(18) "169839000000051003" [6]=> string(18) "169839000000050009" } } [6]=> array(4) { ["group_id"]=> string(18) "169839000000046282" ["display_name"]=> string(11) "nwk devices" ["description"]=> string(0) "" ["monitors"]=> array(4) { [0]=> string(18) "169839000000082009" [1]=> string(18) "169839000000084077" [2]=> string(18) "169839000000084001" [3]=> string(18) "169839000000082229" } } [7]=> array(4) { ["group_id"]=> string(18) "169839000000046013" ["display_name"]=> string(11) "vbr servers" ["description"]=> string(0) "" ["monitors"]=> array(1) { [0]=> string(18) "169839000000047007" } } [8]=> array(4) { ["group_id"]=> string(18) "169839000000054197" ["display_name"]=> string(11) "lnx servers" ["description"]=> string(0) "" ["monitors"]=> array(1) { [0]=> string(18) "169839000000060483" } } [9]=> array(4) { ["group_id"]=> string(18) "169839000000046020" ["display_name"]=> string(11) "vsp servers" ["description"]=> string(0) "" ["monitors"]=> array(7) { [0]=> string(18) "169839000000060177" [1]=> string(18) "169839000000060170" [2]=> string(18) "169839000000060088" [3]=> string(18) "169839000000060095" [4]=> string(18) "169839000000060102" [5]=> string(18) "169839000000060109" [6]=> string(18) "169839000000054102" } } [10]=> array(4) { ["group_id"]=> string(18) "169839000000046058" ["display_name"]=> string(11) "wnd servers" ["description"]=> string(0) "" ["monitors"]=> array(2) { [0]=> string(18) "169839000000066001" [1]=> string(18) "169839000000063119" } } [11]=> array(4) { ["group_id"]=> string(18) "169839000000128001" ["display_name"]=> string(11) "tpt servers" ["description"]=> string(0) "" ["monitors"]=> array(8) { [0]=> string(18) "169839000000143041" [1]=> string(18) "169839000000148017" [2]=> string(18) "169839000000127035" [3]=> string(18) "169839000000123003" [4]=> string(18) "169839000000126011" [5]=> string(18) "169839000000122011" [6]=> string(18) "169839000000129001" [7]=> string(18) "169839000000158028" } } } }
use foreach
loop display following data:
- group_id
- display_name
- description ,
monitors(as comma separated list)
$mongrps_array = json_decode($mongrps_json, true); foreach($mongrps_array['data'] $arr){ foreach($arr $key => $value){ if($key == "monitors"){ echo $key . ": " . implode(", ", $value) . "<br />"; }else{ echo $key . ": " . $value . "<br />"; } } echo "<br />"; }
Comments
Post a Comment