Files
php-question/Question07-Array/Ex08.php
2026-10-06 12:10:09 +09:00

43 lines
1.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
*
* 商品データ(カテゴリーごとの売上金額)
* TODO: 連想配列 $sales を定義し、以下のデータを格納してください。
* カテゴリーごとに複数の商品情報が格納された配列とし、各商品の情報は連想配列で表します。
*
* - "Electronics" カテゴリー:
* - Laptop: price = 120000, quantity = 15
* - Smartphone: price = 80000, quantity = 30
* - Tablet: price = 50000, quantity = 20
*
* - "Clothing" カテゴリー:
* - T-Shirt: price = 3000, quantity = 200
* - Jeans: price = 6000, quantity = 150
* - Jacket: price = 15000, quantity = 100
*
* - "Groceries" カテゴリー:
* - Rice: price = 2000, quantity = 500
* - Milk: price = 180, quantity = 1000
* - Eggs: price = 250, quantity = 800
**/
// 各カテゴリーの合計売上を計算
$categorySales = [];
// TODO: foreach を使って、各カテゴリーごとの合計売上金額を計算してください。
// 各商品の売上金額は、"price" × "quantity" で求めます。
// 計算結果は、連想配列 $categorySales に格納してください。
// (キーはカテゴリー名、値はそのカテゴリーの合計売上金額)
// 最高売上のカテゴリーを見つける
$maxCategory = "";
$maxSales = 0;
// TODO: foreach を使って、$categorySales 配列から最も売上が高いカテゴリーを抽出してください。
// 抽出したカテゴリー名と、その売上金額を、それぞれ $maxCategory と $maxSales に格納すること。
// 結果を表示
echo "最高売上のカテゴリーは「{$maxCategory}」で、売上は " . number_format($maxSales) . " 円です。";