This may be simple to some of you guys but I'm really dummy in php. All what I need is just to transform a multidimensional array and calculate some of it's sub array values.
Array
(
[0] => Array
(
[date] => 2014-10-30
[mission] => one
[point] => 10
)
[1] => Array
(
[date] => 2014-10-31
[mission] => five
[point] => 10
)
[2] => Array
(
[date] => 2014-11-19
[mission] => one
[point] => 8
)
And the output would be like:
Array
(
[one] => Array
(
[mission] => one
[point] => 18 // sum all points where [mission] => one
[count] => 2
[everage] => 9 // 18/2
)
[five] => Array
(
[mission] => five
[point] => 10
[count] => 1
[everage] => 10
)
It is simple to get the sum of [point] values using foreach but troubles begin when I try to get count of arrays with same [mission] value. Thats my code:
foreach($missionsarray as $row) {
if(!isset($newarray[ $row['mission'] ])) {
$newarray[ $row['mission'] ] = $row;
$newarray[ $row['mission'] ]['count'] = count($row['point']);
continue ;
}
$newarray[ $row['mission'] ]['point'] += $row['point'];
}
print_r($newarray);