某一日,須要查詢訂單表中一個月天天的金額數算法
查詢出數據以下:數組
array(14) { [0] => array(2) { ["money"] => string(7) "2000.00" ["times"] => string(2) "12" } [1] => array(2) { ["money"] => string(7) "6000.00" ["times"] => string(2) "14" } [2] => array(2) { ["money"] => string(8) "31569.00" ["times"] => string(2) "15" } [3] => array(2) { ["money"] => string(9) "134596.50" ["times"] => string(2) "16" } [4] => array(2) { ["money"] => string(7) "4000.00" ["times"] => string(2) "17" } [5] => array(2) { ["money"] => string(8) "20525.00" ["times"] => string(2) "18" } [6] => array(2) { ["money"] => string(7) "2000.00" ["times"] => string(2) "20" } [7] => array(2) { ["money"] => string(7) "6000.00" ["times"] => string(2) "21" } [8] => array(2) { ["money"] => string(7) "9000.00" ["times"] => string(2) "22" } [9] => array(2) { ["money"] => string(7) "2000.00" ["times"] => string(2) "23" } [10] => array(2) { ["money"] => string(7) "2000.00" ["times"] => string(2) "25" } [11] => array(2) { ["money"] => string(7) "7000.00" ["times"] => string(2) "26" } [12] => array(2) { ["money"] => string(8) "10000.00" ["times"] => string(2) "27" } [13] => array(2) { ["money"] => string(9) "212263.28" ["times"] => string(2) "28" } }
只查詢了14天的數據,並且中間還有幾天沒有數據,可是我想要沒查詢的天數顯示爲0,這個時候就須要先給數據加個起點!若是沒有1號,咱們就把一號加上,有就略過! 而後還要加個終點,終點應放在原始數組中,否則算法不可能自做主張的添加數據,我這裏默認每一個月31號,畢竟要作折線統計圖,日期是固定的,方法以下:spa
1 if ($list[0]['times'] !=1){ 2 array_unshift($list,array('money' => '0', 'times' =>'01')); //這樣就添加了起點 3 } 4 5 if ($list[count($list)-1]['times'] !=31){ 6 $list[] = array('money' => '0', 'times' => '31');//這樣就添加了終點 7 } 8 9 10 $last = '1'; 11 12 foreach($list as $r) { 13 while($last && $last < $r['times']) { 14 $res[] = array('money' => '0','times' => "$last"); 15 16 $last +=1; 17 } 18 $res[] = $r; 19 $last +=1; 20 } 21 22 23 // $res就是最終的結果 24 dump($res);
結果以下:code
array(31) { [0] => array(2) { ["money"] => string(1) "0" ["times"] => string(2) "01" } [1] => array(2) { ["money"] => string(1) "0" ["times"] => string(1) "2" } [2] => array(2) { ["money"] => string(1) "0" ["times"] => string(1) "3" } [3] => array(2) { ["money"] => string(1) "0" ["times"] => string(1) "4" } [4] => array(2) { ["money"] => string(1) "0" ["times"] => string(1) "5" } [5] => array(2) { ["money"] => string(1) "0" ["times"] => string(1) "6" } [6] => array(2) { ["money"] => string(1) "0" ["times"] => string(1) "7" } [7] => array(2) { ["money"] => string(1) "0" ["times"] => string(1) "8" } [8] => array(2) { ["money"] => string(1) "0" ["times"] => string(1) "9" } [9] => array(2) { ["money"] => string(1) "0" ["times"] => string(2) "10" } [10] => array(2) { ["money"] => string(1) "0" ["times"] => string(2) "11" } [11] => array(2) { ["money"] => string(7) "2000.00" ["times"] => string(2) "12" } [12] => array(2) { ["money"] => string(1) "0" ["times"] => string(2) "13" } [13] => array(2) { ["money"] => string(7) "6000.00" ["times"] => string(2) "14" } [14] => array(2) { ["money"] => string(8) "31569.00" ["times"] => string(2) "15" } [15] => array(2) { ["money"] => string(9) "134596.50" ["times"] => string(2) "16" } [16] => array(2) { ["money"] => string(7) "4000.00" ["times"] => string(2) "17" } [17] => array(2) { ["money"] => string(8) "20525.00" ["times"] => string(2) "18" } [18] => array(2) { ["money"] => string(1) "0" ["times"] => string(2) "19" } [19] => array(2) { ["money"] => string(7) "2000.00" ["times"] => string(2) "20" } [20] => array(2) { ["money"] => string(7) "6000.00" ["times"] => string(2) "21" } [21] => array(2) { ["money"] => string(7) "9000.00" ["times"] => string(2) "22" } [22] => array(2) { ["money"] => string(7) "2000.00" ["times"] => string(2) "23" } [23] => array(2) { ["money"] => string(1) "0" ["times"] => string(2) "24" } [24] => array(2) { ["money"] => string(7) "2000.00" ["times"] => string(2) "25" } [25] => array(2) { ["money"] => string(7) "7000.00" ["times"] => string(2) "26" } [26] => array(2) { ["money"] => string(8) "10000.00" ["times"] => string(2) "27" } [27] => array(2) { ["money"] => string(9) "212263.28" ["times"] => string(2) "28" } [28] => array(2) { ["money"] => string(1) "0" ["times"] => string(2) "29" } [29] => array(2) { ["money"] => string(1) "0" ["times"] => string(2) "30" } [30] => array(2) { ["money"] => string(1) "0" ["times"] => string(2) "31" } }
一樣的. 12個月也能夠這樣操做,只要把終點改成12就能夠了blog