【发布时间】:2018-04-13 08:00:54
【问题描述】:
我使用以下代码编写了此代码:
https://github.com/influxdata/influxdb-php
https://www.highcharts.com/docs/working-with-data/live-data
<?php
include('/opt/lampp/htdocs/example/vendor/autoload.php');
$host = '127.0.0.1';
$port = 8086;
$dbname = 'aTimeSeries';
/*
$client = new \InfluxDB\Client($host, $port);
$database = $client->selectDB('aTimeSeries');
*/
//the exact same thing
$database = \InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass@%s:%s/%s', $host, $port, $dbname));
//query of the last value
$result = $database->query('select * from valeurs group by * order by desc limit 1');
//recup le point
$points = $result->getPoints();
// Set the JSON header
header("Content-type: application/json");
// The x value is the current JavaScript time, which is the Unix time multiplied by 1000.
$x = time() * 1000;
// The y value is a random number
//$y = rand(0,100);
$y = $points;
// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>
输出:
[1523603506000,[{"time":"2018-04-13T07:11:45.208943754Z","value":48}]]
但我想要这个:
[1523603506000,48]
或者这个:
[2018-04-13T07:11:45.208943754Z,48]
如果我尝试只输出$points,我只得到了数组的最后一部分,但这也不是我想要的。
PS:如果你有更好的解决方案来做同样的事情,也许用 nodeJS,我一定会听
【问题讨论】:
-
您的
getPoints()做什么/返回?因为我认为那是返回[{"time":"2018-04-13T07:11:45.208943754Z","value":48}]] -
是的。我只想保留最后一个值 48。
-
你不能只访问 JSON 中的
value吗?我对 php 不是很好,但在我看来,应该有一种方法可以通过名称调用值。 ($points.value可能吗?那种方式?) -
关闭@Codeer,其实是
echo $test[0]->value;(JSON中有顶级数组) -
所以解决方案是
$y = json_decode($points); $ret = array($x, $y[0]->value);
标签: php json highcharts influxdb