如何从 influxdb 的数组 JSON 响应中获取 PHP 值

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]]}]}]}';

我需要打印出列 timecount 的名称以及值,以便能够为 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";
}

?>

如果我理解你写的,这将是为 Google 图表输出 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);
?>