drep94225 2012-04-17 10:06
浏览 111
已采纳

将数组中的数据添加到新数组中

I have created an array of data, from which I can loop through specific fields and echo these values out, but what I need to do is add these values to a new array, ultimately allowing me to find the average of the values in the new array. As i've said, I can echo out the data, and I think I've figured how to get the average, if only I can create the new array! Any help would be greatly appreciated as I just can't find the answer anywhere, and I'm running low on talent!

My table contains approx 25 fields, im pulling out a number of rows based on a session variable. In the instance im working on, I need to take just the values from 1 column in the table, and add these to an array. The code below will loop through the values, and echo them out, 1 at a time:-

while ($cdarray=mysql_fetch_array($calldata))   {
echo $cdarray['score_total'];
}

This gives me 25555 which are the 4 values I would expect 25, 5, 5, 5

I've tried

while ($cdarray=mysql_fetch_array($calldata))   {
$cdts = $cdarray['score_total'];
$cdtsar = array($cdts);
}

Which results in $cdts being assigned a value of 5,

Any help greatly appreciated!!

  • 写回答

3条回答 默认 最新

  • duanmaifu3428 2012-04-17 10:18
    关注

    This will get your data from the array, place it into a new one and calculates the average.

    $cdtsar = array();
    
    while ($cdarray=mysql_fetch_array($calldata))   {
     $cdtsar[] = $cdarray['score_total'];
    }
    
    $average = array_sum($cdtsar) / count($cdtsar);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统