我有一个名为$results
的数组,如下所示,
Array
(
[0] => Array
(
[group_name] => Ist Term - LKG
[group_id] => 69
[parent_group_id] => 0
[amount] => 3200
[group_type] => I SEM TUTION FEES; II SEM TUTION FEES; SPORTS FEES; CONCESSION
[group_amount] => 1000.00; 2000.00; 300.00; -100.00
)
[2] => Array
(
[group_name] => IInd Term - LKG
[group_id] => 70
[parent_group_id] => 0
[amount] => 450.00
[group_type] => MAGAZINE, PHOTOS,BAG, ID etc ; CO - SCHOLASTICS; BOOKS, NOTEBOOKS, UNIFORM
[group_amount] => 200.00; 150.00; 100.00
)
)
我正在表格中显示结果,
<?php echo
'<table class="table table-bordered table-hover">
<tr>
<th>Group Name</th>
<th>Group Type</th>
<th>Group Amount</th>
<th>Total Amount</th>
<th>Actual Pay</th>
</tr>';
foreach($results as $key){
echo
'<tr>
<td>' . $key['group_name'] . '</td>' .
'<td>' . $key['group_type'] . '</td>' .
'<td>' . $key['group_amount'] . '</td>' .
'<td>' . $key['amount'] . '</td>' .
'<td>' . 300 . '</td>' .
'</tr>';
}
echo
'</table>';
?>
表格结构如
此处总金额3200和450各自分别来自组金额的总和,
1000.00; 2000.00; 300.00; -100.00 => 3200
200.00; 150.00; 100.00 => 450
现在,在两个字段中,“实际支付”列的值为300,这意味着用户只能从总金额中支付300,因此我需要根据实际应付款显示值。 假设这里的实际应付款是300,并且组金额需要相应地从现有的组金额中分割出来,如下所示,以及我期望的期望输出,
第一组:
1000, 1900, 0, 0
第二组:
150, 0, 0
我需要根据实际工资在表格中显示如上所示的组数量,需要在组数量内分开。
请帮助我实现上面提到的结果,因为我是PHP的新手,我希望能有更多有用的支持来实现这一目标。任何好的帮助都会更加明显。
答案 0 :(得分:2)
我已更新您的代码并编写了一些逻辑来计算组数量。请尝试这个:
echo
'<table class="table table-bordered table-hover">
<tr>
<th>Group Name</th>
<th>Group Type</th>
<th>Group Amount</th>
<th>Total Amount</th>
<th>Actual Pay</th>
</tr>';
$payamnt = 300;
foreach($results as $key){
$rest_amnt = $key['amount']-$payamnt;
$group_amount = explode(" ",$key['group_amount']);
$bramnt=array();
foreach($group_amount as $gamt){
$bramnt[] = rtrim($gamt,";");
}
$garr='';
if(in_array($rest_amnt,$bramnt)){
//print_r($bramnt);
// echo count($bramnt);
//echo $rest_amnt;
$garr .=$rest_amnt;
for($i=0;$i<count($bramnt)-1;$i++){
$garr .= ', 0';
}
// echo $garr;
}else{
$calval = $rest_amnt-$bramnt[0];
$garr .=$bramnt[0];
$garr .= ', '.$calval;
for($i=0;$i<count($bramnt)-2;$i++){
$garr .= ', 0';
}
//echo $garr;
}
echo
'<tr>
<td>' . $key['group_name'] . '</td>' .
'<td>' . $key['group_type'] . '</td>' .
// '<td>' . $key['group_amount'] . '</td>' .
'<td>' . $garr . '</td>' .
'<td>' . $key['amount'] . '</td>' .
'<td>' . 300 . '</td>' .
'</tr>';
}
echo
'</table>';
输出:
Group Name Group Type Group Amount Total Amount Actual Pay
Ist Term - LKG I SEM TUTION FEES; II SEM TUTION FEES; SPORTS FEES; CONCESSION 1000.00, 1900, 0, 0 3200 300
Ist Term - LKG I SEM TUTION FEES; II SEM TUTION FEES; SPORTS FEES; CONCESSION 150, 0, 0 450 300
答案 1 :(得分:1)
此功能将在(pag)组中划分支付金额(考虑到组中的任何负数)并从每组中减去pag:
function getvals ($values = array(), $paid = 0) {
if ($paid == 0) {
return $values;
}
$paid = abs($paid);
$i = 0;
foreach ($values as $val) {
if ($val <= 0) {
$paid-=$val;
continue;
}
$i++;
}
if ($i == 0) {
return $values;
}
$frm = $paid / $i;
foreach ($values as $val) {
if ($val <= 0) {
$data[] = 0;
} else {
$data[] = round($val - $frm,2);
}
}
return $data;
}
$values = array(1000,2000,300,-100);
$paid = 300;
$data = getvals($values, $paid);
print_r($data);
答案 2 :(得分:1)
请你试试这个我试着用这个逻辑来解决你的问题:
echo
'<table class="table table-bordered table-hover">
<tr>
<th>Group Name</th>
<th>Group Type</th>
<th>Group Amount</th>
<th>Total Amount</th>
<th>Actual Pay</th>
</tr>';
foreach($results[0] as $key){
$new_group_amount = getGroupAmount($key['group_amount'],300);
echo
'<tr>
<td>' . $key['group_name'] . '</td>' .
'<td>' . $key['group_type'] . '</td>' .
'<td>' . $new_group_amount . '</td>' .
'<td>' . $key['amount'] . '</td>' .
'<td>' . 300 . '</td>' .
'</tr>';
}
echo
'</table>';
function getGroupAmount($group_amount,$actual_amount){
$group_amount_array = array_reverse(explode(";",$group_amount));
$balance = $actual_amount;
foreach ($group_amount_array as $group_amount_arr ){
if($balance > 0 ) {
$new_group_amount [] = ($group_amount_arr > 0 && $group_amount_arr - $balance > 0) ? $group_amount_arr - $balance : 0;
}else{
$new_group_amount [] = $group_amount_arr;
}
$balance -=$group_amount_arr;
}
return implode(";",array_reverse($new_group_amount));
}