Are there specific PHP functions or libraries that can be utilized to simplify the process of calculating discounts in scenarios similar to the Harry-Potter Programmier test?
To simplify the process of calculating discounts in scenarios similar to the Harry-Potter Programmier test, you can utilize the `array_count_values` function in PHP to count the occurrences of each book in the basket. Then, you can create a discount logic based on the number of unique books in the basket.
function calculateDiscount($basket) {
$bookCounts = array_count_values($basket);
$totalBooks = count($basket);
$uniqueBooks = count($bookCounts);
$discount = 0;
switch ($uniqueBooks) {
case 2:
$discount = $totalBooks * 0.05;
break;
case 3:
$discount = $totalBooks * 0.1;
break;
case 4:
$discount = $totalBooks * 0.2;
break;
case 5:
$discount = $totalBooks * 0.25;
break;
}
return $discount;
}
$basket = ['Book1', 'Book2', 'Book1', 'Book3', 'Book4', 'Book5'];
$discount = calculateDiscount($basket);
echo "Discount: $discount";
Related Questions
- What advantages does using fopen() over file() offer in terms of performance when reading files in PHP scripts?
- How can SQL queries be optimized to limit the number of results returned for ranking purposes in PHP?
- What are some common functions or methods in PHP for string concatenation and manipulation?