duangewu5234
2013-11-15 14:42
浏览 131
已采纳

获取数组中每第n个值的平均值

I have the following array with they keys being unix timestamps, how can I get the average vlaue for example at an interval of every 30 seconds in a new array with the key being the 30 second interval?

array (size=61)
1375398000 => int 350
1375398015 => int 357
1375398030 => int 354
1375398045 => int 353
1375398060 => int 361
// and so on...

The desired output should be

1375398000 => int 353
1375398030 => int 354

I have tried some logic with using key($array) to get the first value but I cant figure out if this is working correctly inside a foreach loop.

My logic so far

     while($a <= $end){

    $chartData[$a] = $array[$a] //do the average here - current($array) / count($array);

}

I dont know how to get the next set of the keys and values to be used

图片转代码服务由CSDN问答提供 功能建议

我有以下数组,其中键是unix时间戳,我怎样才能得到平均值,例如间隔时间 在一个新数组中每隔30秒,密钥为30秒间隔?</ p>

  array(size = 61)
1375398000 =&gt;  int 350 
1375398015 =&gt;  int 357 
1375398030 =&gt;  int 354 
1375398045 =&gt;  int 353 
1375398060 =&gt;  int 361 
 //等等... 
 </ code> </ pre> 
 
 

所需的输出应为</ p>

  1375398000 =  &GT;  int 353 
1375398030 =&gt;  int 354 
 </ code> </ pre> 
 
 

我尝试使用键($ array)来获取第一个值,但我无法弄清楚这是否在foreach循环中正常工作 。</ p>

到目前为止我的逻辑</ p>

  while($ a&lt; = $ end){
 
 $ chartData [$ a  ] = $ array [$ a] //这里的平均值 - 当前($ array)/ count($ array); 
 
} 
 </ code> </ pre> 
 
 

I 不知道如何获得下一组要使用的键和值</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题