Advertisement
ShadowEmbrace

Orders

Nov 12th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.69 KB | None | 0 0
  1. <?php
  2.  
  3. $productsAndPrices = [];
  4.  
  5. $cmd = explode(' ', readline());
  6.  
  7. while ($cmd[0] !== 'buy') {
  8.     $product = $cmd[0];
  9.     $price = floatval($cmd[1]);
  10.     $quantity = intval($cmd[2]);
  11.  
  12.     if (!key_exists($product, $productsAndPrices)) {
  13.         $productsAndPrices[$product] = [];
  14.         $productsAndPrices[$product][1] = $quantity;
  15.         $productsAndPrices[$product][0] = $price;
  16.     } else {
  17.         $productsAndPrices[$product][0] = $price;
  18.         $productsAndPrices[$product][1] += $quantity;
  19.     }
  20.     $cmd = explode(' ', readline());
  21. }
  22.  
  23. foreach ($productsAndPrices as $products => $priceAndQuantity) {
  24.     printf("%s -> %.2f\n", $products, array_product($priceAndQuantity));
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement