Home How to get total sum of every 10 record using php

# How to get total sum of every 10 record using php

Haidhar Husin
1#
Haidhar Husin Published in 2018-01-11 09:41:53Z
 for example: 10 10 10 10 10 10 10 10 10 10 10 9 OUTPUT 10 10 10 10 10 10 10 10 10 10 ----- 100 next loop 10 9 --- 19
RamRaider
2#
 It's very simple to do using array_chunk and array_sum $arr=array( 10,10,9,10,8,10,7,10,15,10,12,11,10,9,5,21,25,10,15,14,18,12,0,1,2, 13,18,18,18,12,0,1,2,4,5,25,10,7,10,15,10,12,11,10,9,5,21,25,10,15, 14,13,18,18,18,12,0,1,2,4,5,25,10,7,10,15,10,12,11,10,9,5,21,25,10, 15,14,13,18,18,4,5,25,18,12,0,1,2,4,5,25,10,7,10,9,10,8,10,7,10,15 );$size=10; foreach( array_chunk( $arr,$size ) as $chunk ){ printf('%d ', array_sum($chunk ) ); } 
 You can use array_sum and array_splice. Array_splice cuts out part of the input array and array_sum sums the part. $arr = range(1,100); // your array$size = 10; for($i=0;$i