如何平均每100个块的数组 [英] How to average array per 100 chunks
本文介绍了如何平均每100个块的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何将数组拆分为100个块,然后对这些块中的值求平均值.我远不及使用array_chunk,但无法弄清楚如何平均这些块中的值.这只是我数组的一小部分:
I'm wondering how to split an array into 100 chunks and then average the values in those chunks. I came as far as using array_chunk but can't figure out how to average the value in those chunks. This is a small part of my array:
Array ( [0] => 70 [1] => 70 [2] => 70 [3] => 69 [4] => 69 [5] => 69 [6] => 70 [7] => 70 [8] => 70 [9] => 69 [10] => 69 [11] => 70 [12] => 70 [13] => 69 [14] => 70 [15] => 69 [16] => 69 [17] => 70 [18] => 69 [19] => 69 [20] => 69 [21] => 69 [22] => 69 ...
我想要的是以下内容:
[0] => 70(数组中前100个值的平均值)
[0] = > 70 (Average of the first 100 values in the array)
依此类推
预先感谢
推荐答案
$arr = array_chunk(array(1,2,3,4,5,6,7,8,9), 3);
while ($chunk = array_shift($arr)) {
echo array_sum($chunk) / count($chunk) . PHP_EOL;
}
赠予:
2
5
8
如果将其更改为每个块 4
:
And if you change it to 4
per chunk:
2.5
6.5
9
100个块:
$arr = range(1, 16734, 1);
$arr = array_chunk($arr, ceil(count($arr) / 100));
while ($chunk = array_shift($arr)) {
echo array_sum($chunk) / count($chunk) . PHP_EOL;
}
这篇关于如何平均每100个块的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文