Advertisement
dederusliandi

TEST CASE 2 JATIS

May 26th, 2022
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.29 KB | None | 0 0
  1.  
  2. <?php
  3. $cost = 0;
  4. $carbo_per_day = 400;
  5. $foods = [
  6.     "rice" => [
  7.         "price_per_100" => random_int(7, 12),
  8.         "carbohydrate" => 100,
  9.         "gram" => 28,
  10.     ],
  11.     "corn" => [
  12.         "price_per_100" => random_int(7, 12),
  13.         "carbohydrate" => 100,
  14.         "gram" => 21,
  15.     ],
  16.     "potato" => [
  17.         "price_per_100" => random_int(7, 12),
  18.         "carbohydrate" => 100,
  19.         "gram" => 17,
  20.     ]
  21. ];
  22.  
  23. // print_r($foods);
  24. $price_table = [];
  25. foreach($foods as $name=>$food) {
  26.     $food["name"] = $name;
  27.     $item_needed = ceil($carbo_per_day / $food['carbohydrate']);
  28.     $total_gram_food = $item_needed * $food['gram'];
  29.     $price_buy = ($total_gram_food / 100) * $food['price_per_100'];
  30.     array_push($price_table, ["price_buy_per400_carbo" => $price_buy, "gram_total" => $total_gram_food, "sum_food_needed" => $item_needed, "food" => $food]);
  31. }
  32.  
  33. asort($price_table);
  34. // print_r($price_table);
  35. $prices_key = array_keys($price_table);
  36. $best_price_key = $prices_key[0];
  37.  
  38. $output = "Best Lowest Price is " .
  39.     $price_table[$best_price_key]["food"]["name"] .
  40.     " for 400 gram carbohydrat with all total price needed is " .
  41.     $price_table[$best_price_key]["price_buy_per400_carbo"] .
  42.     ". For that price you will get ". $price_table[$best_price_key]["gram_total"] ." grams of " . $price_table[$best_price_key]["food"]["name"];
  43.  
  44. echo $output;
  45.  
  46. ?>
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement