前提・実現したいこと
PHPで下記のようなcsvファイルを読み込ませ、
月(month)ごと+店舗(store)ごとの合計を計算し、
表形式で出したいです。
ちなみにこのフィルはtest.csvと名付け保存しました。
month,store,count,sales 1月,A,123,345 2月,B,23,67 1月,B,111,765 3月,B,435,1234 2月,A,341,987 1月,B,453,900 1月,A,231,700 2月,B,122,499 2月,A,567,1345 3月,A,879,2300
###エラー
Invalid argument supplied for foreach() in test.php on line 36 Warning: Invalid argument supplied for foreach() in test.php on line 82
該当のソースコード
<?php $arrShopSales = array(); foreach ($arrMonth as $month) { foreach ($arrShop as $shop) { $shopSale = [ 'month' => $month, 'shop' => $shop ]; foreach ($arrCol as $col) { $shopSale[$col] = 0; } $arrShopSales[] = $shopSale; } } $file = new SplFileObject('test.csv'); $file->setFlags(SplFileObject::READ_CSV); foreach($file as $res) { if ($res[0] === null) continue; $month = $res[0]; $place = $res[1]; foreach($arrShopSales as $index => $shopSale) { if ($month === $shopSale['month'] && $place === $shopSale['shop']) { $arrShopSales[$index]['product_count'] += $res[2]; $arrShopSales[$index]['sales'] += $res[3]; } } } ?> <table border="1"> <tr> <th>月</th> <th>店舗</th> <th>売上商品数</th> <th>売上(千円)</th> </tr> <?php foreach($arrShopSales as $month => $shopSaleData); ?> <tr> <td rowspan="2"> <?php echo $month; ?> </td> <?php foreach($shopSaleData as $shop => $sale); ?> <td> <?php echo $shop; ?> </td> <td> <?php echo $sale['product_count']; ?> </td> <td> <?php echo $sale['sales']; ?> </td> </tr> </table>
試したこと
上記のように書きましたが、どこが間違えたかがわかりません。
もし、もっとわかりやすい書き方などがありましたら、教えて頂けませんか。
補足情報(FW/ツールのバージョンなど)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。