Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2018
292
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. $items = [];
  3.  while(true) {
  4.      $input = explode(" ", readline());
  5.      if($input[0] == "buy") {
  6.          break;
  7.      }
  8.      $name = $input[0];
  9.      $price = $input[1];
  10.      $qty = $input[2];
  11.       if(!key_exists($name, $items)) {
  12.           $items[$name] = [];
  13.           $items[$name]["price"] = 0.0;
  14.           $items[$name]["qty"] = 0;
  15.       }
  16.       $items[$name]["price"] = (float) $price;
  17.       $items[$name]["qty"] += $qty;
  18.  
  19.  }
  20.  //Math
  21.    foreach($items as $item => $values) {
  22.        $total = $values['price'] * $values['qty'];
  23.        $items[$item] = $total;
  24.    }
  25.  // Output
  26.    foreach($items as $item => $total) {
  27.        printf("%s -> %.2f\n", $item, $total);
  28.    }
  29. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement