- array_sum: documentation ( source)
- array_slice: documentation ( source)
- round: documentation ( source)
<?php
$profits = array(5,7,2,4,7,3,6);
for($i = 0, $length = count($profits); $i < $length; $i++) {
$sixMonthAvg = array_slice($profits, ($i - 5 >= 0 ?$i - 5:0), 6);
echo round(array_sum($sixMonthAvg) / count($sixMonthAvg), 2) . PHP_EOL;
}