首先,我要感谢你们在这件事上帮助了我。这就是我不知道该怎么做的。
我有个时间:
while($result = $s->fetch(PDO::FETCH_OBJ)){
$Itinerario = $result->Itinerario;
$Tarifa = $result->ValorTarifa;
$Distancia = $result->DistanciaItinerario;
$siglaBase = wordwrap($Itinerario, 3, "-", true);
$novoArrayYield[] = array("base"=>$siglaBase, "yield"=>number_format(($Tarifa / $Distancia), 3, '.', ''));
}在打印以下结果的同时:
[0] => Array
(
[base] => RAO-ROO
[yield] => 0.224
)
[1] => Array
(
[base] => RAO-ROO
[yield] => 0.224
)
[2] => Array
(
[base] => RAO-ROO
[yield] => 0.337
)
[3] => Array
(
[base] => SJP-GRU-GRU-UDI-UDI-RAO-RAO-ROO
[yield] => 0.132
)
[4] => Array
(
[base] => BSB-RAO-RAO-ROO
[yield] => 0.476
)
[5] => Array
(
[base] => SJP-GRU-GRU-UDI-UDI-RAO-RAO-ROO
[yield] => 0.176
)
[6] => Array
(
[base] => GIG-RAO-RAO-ROO
[yield] => 0.194
)我一直在尝试创建一个新数组,其结果是:
如果基数是RAO,我需要将所有的收益率值之和,除以我看到的RAO的次数,才能得到一个平均的产量结果。这适用于所有其他可能不同的基地。在这种情况下,它应该是:RAO= (0.224 + 0.224 + 0.337) /3倍
我希望得到这样的结果:
[0] => Array
(
[base] => RAO-ROO
[yield] => 0.261
)
[1] => Array
(
[base] => SJP-GRU-GRU-UDI-UDI-RAO-RAO-ROO
[yield] => 0.154
)
[2] => Array
(
[base] => BSB-RAO-RAO-ROO
[yield] => 0.476
)
[3] => Array
(
[base] => GIG-RAO-RAO-ROO
[yield] => 0.194
)到目前为止我得到了这个:
$newArray = array();
while ( $row = array_shift($novoArrayYield) ) {
if ( !array_key_exists( $row['base'], $newArray ) ) {
$newArray[$row['base']] = $row;
} else {
$newArray[$row['base']]['yield'] += $row['yield'];
}
}
print "<pre>";
print_r( $newArray );
print "</pre>";现在,我需要计算出,如何计算每个基地出现多少次,并将其收益率除以这个数量的次数,才能得到一个平均值。
有什么帮助吗?再次感谢您!
发布于 2015-08-12 15:19:55
你必须先把所有的收益组合起来,所以你知道要除以多少,试着做这样的测试。
while ( $row = array_shift($novoArrayYield) ) {
$groupedBases[$row['base']][] = $row['yield'];
}
foreach ($groupedBases as $base => $yields) {
var_dump(sprintf('Base: %s | Average: %2.3f', $base, (array_sum($yields) / count($yields))));
}https://stackoverflow.com/questions/31967279
复制相似问题