php 二维数组分组求平均值放入一个新数组
array(array('weight'=>'55','time'=>'2014-02-03'),array('weight'=>'75','time'=>'2014-0...
array(
array('weight' => '55','time'=>'2014-02-03'),
array('weight' => '75','time'=>'2014-02-03'),
array('weight' => '65','time'=>'2014-02-03'),
array('weight' => '72','time'=>'2014-04-12'),
array('weight' => '25','time'=>'2014-04-12')
),
求在同一个time 下weight的平均值 然后组成一个新数组 展开
array('weight' => '55','time'=>'2014-02-03'),
array('weight' => '75','time'=>'2014-02-03'),
array('weight' => '65','time'=>'2014-02-03'),
array('weight' => '72','time'=>'2014-04-12'),
array('weight' => '25','time'=>'2014-04-12')
),
求在同一个time 下weight的平均值 然后组成一个新数组 展开
2014-09-18
展开全部
<?php
$arr1 = array(
array('weight' => '55','time'=>'2014-02-03'),
array('weight' => '75','time'=>'2014-02-03'),
array('weight' => '65','time'=>'2014-02-03'),
array('weight' => '72','time'=>'2014-04-12'),
array('weight' => '25','time'=>'2014-04-12')
);
$arr2 = array();
foreach ($arr1 as $value) {
$arr2[$value['time']][] = $value['weight'];
}
$arr3 = array();
foreach($arr2 as $key => $value){
$arr3[$key] = array_sum($value) / count($value);
}
var_dump($arr3);
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询