Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $cost = 0;
- $carbo_per_day = 400;
- $foods = [
- "rice" => [
- "price_per_100" => random_int(7, 12),
- "carbohydrate" => 100,
- "gram" => 28,
- ],
- "corn" => [
- "price_per_100" => random_int(7, 12),
- "carbohydrate" => 100,
- "gram" => 21,
- ],
- "potato" => [
- "price_per_100" => random_int(7, 12),
- "carbohydrate" => 100,
- "gram" => 17,
- ]
- ];
- // print_r($foods);
- $price_table = [];
- foreach($foods as $name=>$food) {
- $food["name"] = $name;
- $item_needed = ceil($carbo_per_day / $food['carbohydrate']);
- $total_gram_food = $item_needed * $food['gram'];
- $price_buy = ($total_gram_food / 100) * $food['price_per_100'];
- array_push($price_table, ["price_buy_per400_carbo" => $price_buy, "gram_total" => $total_gram_food, "sum_food_needed" => $item_needed, "food" => $food]);
- }
- asort($price_table);
- // print_r($price_table);
- $prices_key = array_keys($price_table);
- $best_price_key = $prices_key[0];
- $output = "Best Lowest Price is " .
- $price_table[$best_price_key]["food"]["name"] .
- " for 400 gram carbohydrat with all total price needed is " .
- $price_table[$best_price_key]["price_buy_per400_carbo"] .
- ". For that price you will get ". $price_table[$best_price_key]["gram_total"] ." grams of " . $price_table[$best_price_key]["food"]["name"];
- echo $output;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement