[英]PHP adding key to array from database query
Wondering if you can help me. 想知道您是否能帮助我。 I am querying a table to populate a graph with a H axis of time and V axis of count and im trying to dynamically add filters to the graph based on servers in the database table.
我正在查询一个表,以使用H轴时间轴和V轴计数轴填充图形,并尝试基于数据库表中的服务器向该图形动态添加过滤器。 At this moment its static.
此刻其静止。
i use the following code to query database and add the series to the graph 我使用以下代码查询数据库并将系列添加到图中
$results = array();
foreach( \IPS\Db::i()->select( '*', 'stats', $where, 'time ASC' ) as $row )
{
$value = '_'.$row['server'];
if( !isset( $results[ $row['time'] ] ) )
{
$results[ $row['time'] ] = array(
'time' => $row['time'],
'_64' => 0,
'_66' => 0
);
}
//$results[ $row['time'] ][$value] = 0;
if ($value == '_64')
{
$results[ $row['time'] ]['_64'] = $row['value_1'];
}
elseif($value == '_66')
{
$results[ $row['time'] ][ '_66' ] = $row['value_1'];
}
}
return $results;
and add the filters 并添加过滤器
$chart->addSeries('_66', 'number');
$chart->addSeries('_64', 'number');
I have tried to make it dynamic by doing 我试图通过做使其动态
foreach(\IPS\Db::i()->query( "SELECT DISTINCT server FROM stats ORDER BY server" ) as $row)
{
$value = '_'.$row['server'];
$chart->addSeries($value, 'number');
}
and 和
$value = '_'.$row['server'];
if( !isset( $results[ $row['time'] ] ) )
{
$results[ $row['time'] ] = array(
'time' => $row['time'],
$value => 0,
);
}
$results[ $row['time'] ][$value] = $row['value_1'];
But this did not work in any way. 但这没有任何作用。 I think im going about adding to the array completely wrong.
我认为即时添加到数组完全错误。 Any suggestions please?
有什么建议吗?
array should look like this 数组应该看起来像这样
array (size=8)
1504025011 =>
array (size=3)
'time' => int 1504025011
'_64' => int 2
'_66' => int 0
1504094803 =>
array (size=3)
'time' => int 1504094803
'_64' => int 0
'_66' => int 14
but instead 但反而
array (size=8)
1504025011 =>
array (size=2)
'time' => int 1504025011
'_64' => int 2
1504094803 =>
array (size=2)
'time' => int 1504094803
'_66' => int 14
I managed to make it work in the end. 我设法使它最终运行。 Was a simple in the end.
最后很简单。 (Normally the case) Sure i tried this before and didn't work.
(通常是这样)确定我以前曾尝试过这种方法,但是没有用。 Must have missed something.
一定错过了什么。
I used this code to add keys/value to the array. 我使用此代码将键/值添加到数组。
foreach(\IPS\Db::i()->query( "SELECT DISTINCT(server) AS Server FROM stats ORDER BY Server DESC;" ) as $rows)
{
$value = '_' .$rows['Server'];
$results[ $row['time'] ][$value] = 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.