php query to array,PHP從數據庫查詢中向數組添加鍵

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. At this moment its static.

想知道你是否可以幫助我。我正在查詢一個表來填充一個H軸為時間軸和V軸計數的圖形,我試圖根據數據庫表中的服務器動態地將過濾器添加到圖形中。此刻它的靜止。

ba106c6beca53d8d2d44a385f919bc84.jpe

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

1 个解决方案

#1

1

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值