2
votes
@foreach($group as $key=>$data)
<td>{{$data->value}}</td>
@endforeach

For example:

value1 = 1

value2 = 2

value3 = 3

value4 = 4

value5 = 5

value6 = 6

value7 = 7

value8 = 8

value9 = 9

value10 = 10

value11 = 11

value12 = 12

I want to count $data every 3 time loop, so the result is

result lopping1 =6

result lopping2 =15

result lopping3 =24

result lopping4 =33

note:

6 = 1+2+3

15 = 4+5+6

24 = 7+8+9

33 = 10+11+12

What should i do?

4

4 Answers

1
votes

This is Blade Syntax answer:

@foreach(array_chunk($group, 3) as $value)
    <td>
        @php
            echo array_reduce($value, function($sum, $item) {
                    $sum +=$item->value;
                    return $sum;
                })
        @endphp
    </td>
@endforeach

or pure PHP:

<?php
$groupSum = [];
foreach (array_chunk($group, 3) as $value) {
    $groupSum[] = array_reduce($value, function ($sum, $item) {
        $sum += $item->value;
        return $sum;
    });
}

print_r($groupSum);

All this because there are objects in author question.

0
votes

you need to count iteration and save it.something like this.

$collection=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$count=1;
$result=array();
foreach ($collection as $value) {
    $result[]=$value;
    $count++;
    if ($count==4) {

    $data=array_sum($result);
    $result = array_map(function($v){ return 0; }, $result);
    $count=1;
    echo $data;
    echo '<br>';
}
} 
0
votes

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>";
}
0
votes

You can use array_chunk and array_sum

$array_number = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

$test = array_chunk($array_number, 3);
$i = 0;
while($i < count($test)){
    echo array_sum($test[$i]);
    $i++;
}

For you, maybe below option works

@foreach ($group->chunk(3) as $chunk)
    {{$chunk->sum()}}
@endforeach