3 回答
TA贡献1829条经验 获得超9个赞
<?php
$credit_to_spend = 25;
$available_credit = array(10,10,10);
$used_credit = 0;
for( $i=0; $i<count($available_credit); $i++ ){
if( $credit_to_spend > $used_credit ){
$required = ($credit_to_spend - $used_credit);
$used_credit += ($required < $available_credit[$i]) ? $required : $available_credit[$i];
$remaining = (($required < $available_credit[$i]) ? ($available_credit[$i] - $required): ($credit_to_spend - $used_credit));
echo "Point: " . $available_credit[$i]
. " Used: " . $used_credit
. " Remaining: " . $remaining;
}
echo "<br>";
}
TA贡献1810条经验 获得超4个赞
您可能正在寻找类似的东西:
<?php
$content_credit = 25;
$data = array(10,10,10);
$total = array_sum($data);
$used_credit = 0;
for($i=0; $i<count($data); $i++){
if($content_credit > $used_credit){
$used_credit += $data[$i];
echo "Point: " . $data[$i];
echo " Used: " . $used_credit;
$remaining = $used_credit > $content_credit ? $total - $content_credit : $total - $used_credit;
echo " Remaining: " . $remaining;
}
echo "<br>";
}
你需要知道你总共有多少信用来计算余额。
$used_credit要获得实际使用的信用额度,只需知道和之间哪个值更小就足够了$content_credit。所以你可以min()用来获取价值。
echo " Used: " . min($used_credit, $content_credit);
类似地,$remaining可以用max()三元运算符结果的值或简单的计算来代替$total - $actual_used。
echo " Remaining: " . max($total - $content_credit, $total - $used_credit);
- 3 回答
- 0 关注
- 104 浏览
添加回答
举报