【发布时间】:2021-01-11 06:42:54
【问题描述】:
我想在 foreach 循环中返回匹配项的值。我已经尝试过使用打击代码,但它没有正确返回。如果与最后一项匹配,则不会返回正确的值。
//$bonuses = count => amount
Array
(
[15] => 25
[30] => 50
[46] => 100
)
// getting keys to compare the count
$counts = array_keys($bonuses);
foreach ($bonuses as $count => $bonus) {
if ($total_number_of_products <= next($counts)) {
$tti = 'Items: '. $total_number_of_products. ' Bonus: '. $bonus. '<BR/>';
}
}
如果小于或等于,循环应该返回项目。如果计数为46 或更高(在这种情况下),则输出应为100。现在它正在返回50。
【问题讨论】:
-
那么10个产品的奖金是0? 40 就是 50?
-
没错。这是一个固定的奖金。只有当计数达到数字时,用户才会得到。没有中间人。