回答編集履歴

1

修正

2019/03/11 05:34

投稿

papinianus
papinianus

スコア12705

test CHANGED
@@ -1,6 +1,10 @@
1
- バグ原因は分かりませんが、$arrShopSalesの作りかと代入が変な気がする
1
+ 変数誤記などあったので全部書き換えしました。
2
+
3
+ 質問者様のコメントは、foreachの行の最後が:ではなく;になっているため、foreachの中で何もしないことにされており、htmlのテーブルがループせずに、foreachの最後の値だけが出力されていた、という現象だと思います。
2
4
 
3
5
  ```php
6
+
7
+ <?php
4
8
 
5
9
  $arrShopSales = [];
6
10
 
@@ -20,9 +24,43 @@
20
24
 
21
25
  }
22
26
 
27
+
28
+
29
+
30
+
31
+ $file =<<<EOF
32
+
33
+ 1月,A,123,345
34
+
35
+ 2月,B,23,67
36
+
37
+ 1月,B,111,765
38
+
39
+ 3月,B,435,1234
40
+
41
+ 2月,A,341,987
42
+
43
+ 1月,B,453,900
44
+
45
+ 1月,A,231,700
46
+
47
+ 2月,B,122,499
48
+
49
+ 2月,A,567,1345
50
+
51
+ 3月,A,879,2300
52
+
53
+ EOF;
54
+
55
+ $file = explode("\n", $file);
56
+
57
+
58
+
23
59
  //↓こう使う
24
60
 
25
61
  foreach($file as $res) {
62
+
63
+ $res = explode(",", $res);
26
64
 
27
65
  if ($res[0] === null) continue;
28
66
 
@@ -32,12 +70,66 @@
32
70
 
33
71
 
34
72
 
35
- $arrShopSales[$month][$shop]['product_count'] += $res[2];
73
+ $arrShopSales[$month][$place]['product_count'] += $res[2];
36
74
 
37
- $arrShopSales[$month][$shop]['product_count'] += $res[3];
75
+ $arrShopSales[$month][$place]['sales'] += $res[3];
38
76
 
39
77
  }
40
78
 
79
+ ?>
80
+
81
+
82
+
83
+ <table border="1">
84
+
85
+ <tr>
86
+
87
+ <th>月</th>
88
+
89
+ <th>店舗</th>
90
+
91
+ <th>売上商品数</th>
92
+
93
+ <th>売上(千円)</th>
94
+
95
+ </tr>
96
+
97
+ <?php foreach($arrShopSales as $month => $shopSaleData): ?>
98
+
99
+ <tr>
100
+
101
+ <td rowspan="2">
102
+
103
+ <?php echo $month; ?>
104
+
105
+ </td>
106
+
107
+ <?php foreach($shopSaleData as $shop => $sale): ?>
108
+
109
+ <td>
110
+
111
+ <?php echo $shop; ?>
112
+
113
+ </td>
114
+
115
+ <td>
116
+
117
+ <?php echo $sale['product_count']; ?>
118
+
119
+ </td>
120
+
121
+ <td>
122
+
123
+ <?php echo $sale['sales']; ?>
124
+
125
+ </td>
126
+
127
+ </tr>
128
+
129
+ <?php endforeach;?>
130
+
131
+ <?php endforeach;?>
132
+
133
+ </table>
134
+
41
135
  ```
42
-
43
- 出力はかえなくても↑に合ってる気がする