返回匹配项的PHP foreach循环
我想在foreach循环中返回匹配项的值。我试过使用blow代码,但返回不正确。如果与最后一项匹配,它将不会返回正确的值返回匹配项的PHP foreach循环,php,arrays,loops,foreach,Php,Arrays,Loops,Foreach,我想在foreach循环中返回匹配项的值。我试过使用blow代码,但返回不正确。如果与最后一项匹配,它将不会返回正确的值 //$bonuses = count => amount Array ( [15] => 25 [30] => 50 [46] => 100 ) // getting keys to compare the count $counts = array_keys($bonuses); foreach ($bonuses as
//$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/>';
}
}
/$bonensions=count=>金额
排列
(
[15] => 25
[30] => 50
[46] => 100
)
//获取密钥以比较计数
$counts=数组\ U键($bonus);
foreach($count形式的奖金=>$POUNS){
如果($total_number_of_products您在正确的轨道上,但是您只需要在到达下一个关键点之前输出先前的奖金值。这是一种方法:
功能获得奖金($total\u number\u产品){
$bonensions=数组(15=>25,30=>50,46=>100);
$bonus=0;
foreach($num_产品的奖金=>$next_奖金){
如果($total_number_of_products<$num_products)中断;
$bonus=$next_奖金;
}
回报$bonus;
}
示例用法:
foreach([10,20,30,45,46,50]美元产品){
回显“项目:”.$products.“奖金:”。获得奖金($products)。“
”。“\n”;
}
输出:
Items: 10 Bonus: 0<BR/>
Items: 20 Bonus: 25<BR/>
Items: 30 Bonus: 50<BR/>
Items: 45 Bonus: 50<BR/>
Items: 46 Bonus: 100<BR/>
Items: 50 Bonus: 100<BR/>
因此,10个产品的奖金是0?而40个产品的奖金是50?这是正确的。这是一个固定的奖金。只有当计数达到数字时,用户才能得到。中间没有。@CodeLover无需担心-我很高兴我能帮上忙。