You can loop your array for i+3 and add the values accordingly.
If your array has elements in multiples of 3 then you can do this
for($i=2; $i<count($arr); $i+=3) {
$data = ($arr[$i] + $arr[$i-1] + $arr[$i-2]);
echo "<td>".$data."</td>";
}
If your array has elements less than 3 then you can do this
for($i=0; $i<count($arr); $i++) {
$data += $arr[$i];
}
echo "<td>".$data."</td>";
If your array has elements more than 3 but not in multiples of 3 then you can do this
$post = count($arr) % 3;
for($i=2; $i<(count($arr) - $post); $i+=3) {
$data = ($arr[$i-1] + $arr[$i-2] + $arr[$i]);
echo "<td>".$data."</td>";
}
$last = "";
for($i=(count($arr) - $post); $i<count($arr); $i++) {
$last += $arr[$i];
}
echo "<td>".$last."</td>";
By combining all conditions
if (count($arr) < 3) {
$data = "";
for($i=0; $i<count($arr); $i++) {
$data += $arr[$i];
}
echo "<td>".$data."</td>";
}else if (count($arr) % 3 == 0) {
for($i=2; $i<count($arr); $i+=3) {
$data = ($arr[$i-1] + $arr[$i-2] + $arr[$i]);
echo "<td>".$data."</td>";
}
}else{
$post = count($arr) % 3;
for($i=2; $i<(count($arr) - $post); $i+=3) {
$data = ($arr[$i-1] + $arr[$i-2] + $arr[$i]);
echo "<td>".$data."</td>";
}
$last = "";
for($i=(count($arr) - $post); $i<count($arr); $i++) {
$last += $arr[$i];
}
echo "<td>".$last."</td>";
}