[英]How get in PHP value from array JSON response of influxdb
如果已经有类似的问题,我提前道歉。 我试图找到一个解决方案,但不幸的是我仍然找不到。
我有来自 influxdb 的 JSON 响应
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [ ["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],[" 2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}] }';
我需要打印出列的名称time
和count
以及值,以便能够为 Google 图表生成一个新的 JSON。
我现在能做的只是得到一个statement_id=0
。
$decode = json_decode($output2,true);
foreach($decode['results'] as $val){
//Actual statement_id is 0
echo "statement_id is ";
echo $val['statement_id'];
echo "<br/>";
}
但我需要打印:
time, count<br/>
2020-07-02T00:00:00Z,1<br/>
2020-07-03T00:00:00Z,0<br/>
2020-07-04T00:00:00Z,0<br/>
2020-07-05T00:00:00Z,0<br/>
2020-07-06T00:00:00Z,1<br/>
或将其放入变量中以便能够使用它。 我已经尝试过不转换为数组
$decode = json_decode($output2); //without ",true"
你能帮我解决一下吗。
我几乎一整天都在寻找解决方案,但我找不到解决问题的方法。
如果您需要在 html 上打印出来,只需将 \n 更改为<br>
<?php
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],["2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}]}';
$decode = json_decode($output2,true);
echo $decode["results"][0]["series"][0]["columns"][0].",".$decode["results"][0]["series"][0]["columns"][1]."\n";
foreach($decode["results"][0]["series"][0]["values"] AS $el)
{
echo $el[0].",".$el[1];
echo "\n";
}
?>
如果我明白你写这将是代码 output JSON 为谷歌图表:
<?php
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],["2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}]}';
$decode = json_decode($output2,true);
$googlechart = array();
$googlechart[] = array($decode["results"][0]["series"][0]["columns"][0],$decode["results"][0]["series"][0]["columns"][1]);
foreach($decode["results"][0]["series"][0]["values"] AS $el)
{
$googlechart[] = array($el[0],$el[1]);
}
echo json_encode($googlechart);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.