i've have array such
//$ary, $ary contains 80 entries, i'm showing first 10 array ( [0] => array ( [december 2012] => 58 ) [1] => array ( [november 2012] => 84 ) [2] => array ( [december 2012] => 83 ) [3] => array ( [november 2012] => 72 ) [4] => array ( [november 2012] => 47 ) [5] => array ( [december 2012] => 93 ) [6] => array ( [november 2012] => 79 ) [7] => array ( [october 2012] => 70 ) [8] => array ( [november 2012] => 75 ) [9] => array ( [october 2012] => 59 ) [10] => array ( [december 2012] => 67 ) )
i'm able total each month using:
foreach($ary $array) { foreach($array $month=>$cent) { if(isset($abc[$month])) // prevent index warning { $abc[$month] += $cent; // tried using $abc[$month] = $abc[$month]/2 wrong values returned } else { $abc[$month] = $cent; } } }
this resulting in
array ( [december 2012] => 2195 [november 2012] => 2159 [october 2012] => 1631 )
but i'm unable find way find average value each month. in case, there 31 instances of december 2012
, 31 instances of november 2012
, 25 instances of october 2012
. thus, need hold of these 31
, 31
, , 25
values can divide month number.
i'm thinking must done inside if(isset($abc[$month]))
loop capture haven't been successful far. there other way average?
ps : can't hardcode month's name (october, november, december) comparison purposes. keep changing @ regular intervals.
$eachmonth = array(); foreach($ary $array) { foreach($array $month=>$cent) { $eachmonth[$month][] = $cent; if(isset($abc[$month])) // prevent index warning { $abc[$month] += $cent } else { $abc[$month] = $cent; } } }
then each month can calculate average:
foreach( $eachmonth $month => $values) { echo $month.' : '.$abc[$month] / count($values); }
Comments
Post a Comment