我有两个向量。一个有时间和其他速度值。我需要获得相同时间间隔的平均速度值。 离。
$time=array('18:00:00', '18:00:00', '19:15:00', '19:15:00', '20:30:00');
$v=array(10, 20, 30, 70, 60);
我需要获取向量:$v_optimized(15, 50, 60)
;
值“15”是10
中值20
和$v
的平均速度,对应于“18:00:00
”的相同时间,依此类推所有值。
我写过这段代码,但我遗漏了一些东西。请有人帮助我。
$time=array('18:00:00', '18:00:00', '19:15:00', '19:15:00', '20:30:00');
$v=array(10, 20, 30, 70, 60);
$time_opt= array();
$v_opt= array();
$start=0;
$count=0;
$end=0;
$sum=0;
$nr=count($time);
for($i=0; $i<$nr-2; $i++){
for($j=$i+1; $j<$nr-1; $j++){
$start = $i;
if($time[$j]==$time[$j+1]){
$time_opt[$i]=$time[$i];
$count++;
$sum+= $v[$j];
}
else{
$avg = $sum / $count;
array_push($v_opt, $avg);
//$count = 0;
}
}}
for($k=0;$k<count($v_opt); $k++)
echo $v_opt[$k].'<br>';
答案 0 :(得分:1)
据我所知,这就是你想要的......尝试一下
<?php
$time=array('18:00:00', '18:00:00', '19:15:00', '19:15:00', '20:30:00');
$v=array(10, 20, 30, 70, 60);
// get total of vector for a given time
$temp_arr = array();
foreach($time as $key=>$value)
{
$temp_arr[$value] = $temp_arr[$value]+$v[$key] ;
}
// count the repititons
$count_val_time = array_count_values($time);
// your required array
$req_arr = array();
foreach($temp_arr as $key=>$value)
{
$req_arr[$key] = $value/($count_val_time[$key]);
}
print_r($req_arr);
?>