Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $productsAndPrices = [];
- $cmd = explode(' ', readline());
- while ($cmd[0] !== 'buy') {
- $product = $cmd[0];
- $price = floatval($cmd[1]);
- $quantity = intval($cmd[2]);
- if (!key_exists($product, $productsAndPrices)) {
- $productsAndPrices[$product] = [];
- $productsAndPrices[$product][1] = $quantity;
- $productsAndPrices[$product][0] = $price;
- } else {
- $productsAndPrices[$product][0] = $price;
- $productsAndPrices[$product][1] += $quantity;
- }
- $cmd = explode(' ', readline());
- }
- foreach ($productsAndPrices as $products => $priceAndQuantity) {
- printf("%s -> %.2f\n", $products, array_product($priceAndQuantity));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement