Cumulative sum in PHP Array from MySQL Query
我想对MySQL查询中的数据进行累积和。我的SQL查询已经很好地工作了,但我不知道如何累积列"sum a"的总和。我想保留MySQL查询表中的日期,但下一个索引将具有此索引和上一个索引的累积和。
我的mysql查询数组如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | [0] => Array ( [data] => 2017-11-01 [SUMA] => 19 ) [1] => Array ( [data] => 2017-11-02 [SUMA] => 97 ) [2] => Array ( [data] => 2017-11-03 [SUMA] => 296 ) [3] => Array ( [data] => 2017-11-05 [SUMA] => 58 ) [4] => Array ( [data] => 2017-11-06 [SUMA] => 216 ) [5] => Array ( [data] => 2017-11-07 [SUMA] => 194 ) [6] => Array ( [data] => 2017-11-08 [SUMA] => 444 ) [7] => Array ( [data] => 2017-11-09 [SUMA] => 301 ) [8] => Array ( [data] => 2017-11-10 [SUMA] => 213 ) [9] => Array ( [data] => 2017-11-11 [SUMA] => 5 ) [10] => Array ( [data] => 2017-11-12 [SUMA] => 8 ) [11] => Array ( [data] => 2017-11-13 [SUMA] => 186 ) [12] => Array ( [data] => 2017-11-14 [SUMA] => 227 ) [13] => Array ( [data] => 2017-11-15 [SUMA] => 180 ) [14] => Array ( [data] => 2017-11-16 [SUMA] => 225 ) |
)我想要这个数组或新数组如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | [0] => Array ( [data] => 2017-11-01 [SUMA] => 19 ) [1] => Array ( [data] => 2017-11-02 [SUMA] => 116 ) [2] => Array ( [data] => 2017-11-03 [SUMA] => 412 ) [3] => Array ( [data] => 2017-11-05 [SUMA] => 470 ) [4] => Array ( [data] => 2017-11-06 [SUMA] => 686 ) [5] => Array ( [data] => 2017-11-07 [SUMA] => 880 ) [6] => Array ( [data] => 2017-11-08 [SUMA] => 1324 ) [7] => Array ( [data] => 2017-11-09 [SUMA] => 1625 ) [8] => Array ( [data] => 2017-11-10 [SUMA] => 1838 ) [9] => Array ( [data] => 2017-11-11 [SUMA] => 1843 ) [10] => Array ( [data] => 2017-11-12 [SUMA] => 1851 ) [11] => Array ( [data] => 2017-11-13 [SUMA] => 2037 ) [12] => Array ( [data] => 2017-11-14 [SUMA] => 2264 ) [13] => Array ( [data] => 2017-11-15 [SUMA] => 2444 ) [14] => Array ( [data] => 2017-11-16 [SUMA] => 2699 ) |
我试过这样做,但它只显示我最后的日期和完整的累积而不是每个索引。
1 2 3 4 5 | while ($row = mysqli_fetch_assoc($result)) { $myArray['data'] = $row['data']; $myArray['suma'] += $row['SUMA']; } |
创建数组时执行此操作
1 2 3 4 5 6 7 8 9 | $s=0; $myArray = []; while ($row = mysqli_fetch_assoc($result)) { $temp['data'] = $row['data']; $s = $temp['suma'] = $row['SUMA'] + $s; $myArray[] = $temp; } echo '[cc lang="php"]'; print_r($myArray); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <?php $array=array( 0 => Array ( 'data' => '2017-11-01', 'SUMA' => 19 ), 1 => Array ( 'data' => '2017-11-02', 'SUMA' => 97 ), 2 => Array ( 'data' => '2017-11-03', 'SUMA' => 296, ), 3 => Array ( 'data' => '2017-11-05', 'SUMA' => 58, ), ); $i=0; while ($i<count($array) ) { if($i==0) { $newArray[0]['data'] = $array[$i]['data']; $newArray[0]['SUMA'] = $array[$i]['SUMA']; } else{ $newArray[$i]['data']=$array[$i]['data']; $newArray[$i]['SUMA']=$array[$i]['SUMA']+$newArray[$i-1]['SUMA']; } $i++; } echo '[cc lang="php"]'; print_r($newArray); |
输出为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
您可以删除