【发布时间】:2018-04-15 15:15:13
【问题描述】:
我正在为每个循环生成一个值数组。结果看起来有点像我在下面粘贴的数组值。现在我想添加三个迭代的相应值并得到它们的平均值......就像我想要的第 0 个键 - (87+95.2+100)/3 ... 像这样我想要所有的值.. 如何我该怎么做?
这是代码。 $items 包含我在下面粘贴的这些数组值。每个循环有两个,我必须想办法获得平均值。
foreach ($data['data'] as $k => $v)
{
$items = array();
foreach ($data['departments'] as $a => $b)
{
$items[] = $v['departments'][$a]['score'];
}
}
echo "<pre>";
print_r($items);
echo "</pre>";
当我打印出来时,$items 包含以下内容
Array
(
[0] => 87
[1] => 88.90000000000001
[2] => 97.40000000000001
[3] => 27.4
[4] => 94.7
[5] => 91.7
[6] => 93.90000000000001
[7] => 100
[8] => 0
[9] => 100
[10] => 94.40000000000001
[11] => 90
[12] => 100
[13] => 78.59999999999999
[14] => 63.3
[15] => 97.40000000000001
[16] => 96.90000000000001
[17] => 97
)
Array
(
[0] => 95.2
[1] => 94.7
[2] => 95
[3] => 33.6
[4] => 94.8
[5] => 100
[6] => 92.3
[7] => 78.59999999999999
[8] => 92.3
[9] => 96.40000000000001
[10] => 92.5
[11] => 100
[12] => 96.3
[13] => 84.09999999999999
[14] => 63
[15] => 97.7
[16] => 94.3
[17] => 97.09999999999999
)
Array
(
[0] => 100
[1] => 95
[2] => 91.90000000000001
[3] => 33.6
[4] => 98.2
[5] => 96.3
[6] => 97.90000000000001
[7] => 86.7
[8] => 91.7
[9] => 96.59999999999999
[10] => 92.5
[11] => 100
[12] => 92.3
[13] => 83.3
[14] => 63.6
[15] => 92.90000000000001
[16] => 94.40000000000001
[17] => 98.59999999999999
)
【问题讨论】:
标签: php arrays foreach array-push