首先我要感谢大家在这件事上对我的帮助。这是我不知道如何执行的。
我有这个while:
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, '.', ''));
}
然后while输出如下结果:
[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-ROO,则需要将所有yield值相加,然后除以我看到RAO-ROO的次数,以获得平均yield结果。这适用于所有其他可能不同的碱基。在这种情况下,它应该是:RAO-ROO =(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>";
现在我需要弄清楚如何计算每个碱基出现的次数,并将其产量除以这个次数,得到平均值。
请帮忙好吗?再次感谢!你必须先把所有的产量分组,这样你就知道要除以多少,试试这样做[test]:
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))));
}