我在每个循环中为a生成一个值数组。结果看起来有点像我下面粘贴的数组值。现在我想添加三个迭代的相应值并获得它们的平均值...就像我想要的第0个键 - (87 + 95.2 + 100)/ 3 ...像这样我想要所有的值..如何我该怎么做?
这是代码。 $ items包含我在下面粘贴的这些数组值。每个循环有两个,我必须找到一种方法来获得平均值。
tee
当我打印出来时,$ 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>";
答案 0 :(得分:0)
<?php
$items = array();
foreach ($data['data'] as $k => $v) {
foreach ($data['departments'] as $a => $b) {
$items[$k][] = $v['departments'][$a]['score'];
}
}
$res = array_map('average', zip($items));
var_dump($res);
function zip($arrays) {
$zip = [];
if ($arrays) {
$count = count(current($arrays));
for ($i = 0; $i < $count; $i++) {
foreach ($arrays as $array) {
$zip[$i][] = $array[$i];
}
}
}
return $zip;
}
function average($numbers) {
return array_sum($numbers) / count($numbers);
}