如何在php中对组中的值求和?
Posted
技术标签:
【中文标题】如何在php中对组中的值求和?【英文标题】:How to sum the values in a group in php? 【发布时间】:2018-09-27 01:27:59 【问题描述】:我列出了一个学生的费用详情,代码是,
<table class="table table-hover">
<thead>
<tr>
<th>Fees Group</th>
<th>Fees Type</th>
<th>Fees Amount</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$total_amount = "0";
$total_deposite_amount = "0";
$total_fine_amount = "0";
$total_discount_amount = "0";
$total_balance_amount = "0";
$group_total = 0;
$group_id = array();
foreach ($student_due_fee as $key => $fee)
foreach ($fee->fees as $fee_key => $fee_value)
$fee_paid = 0;
$fee_discount = 0;
$fee_fine = 0;
$alot_fee_discount = 0;
$group_id = $fee_value->fee_groups_id;
if ($group_id !== $finalGroupId)
$group_total += $fee_value->amount;
echo $group_total;
if (!empty($fee_value->amount_detail))
$fee_deposits = json_decode(($fee_value->amount_detail));
foreach ($fee_deposits as $fee_deposits_key => $fee_deposits_value)
$fee_paid = $fee_paid + $fee_deposits_value->amount;
$fee_discount = $fee_discount + $fee_deposits_value->amount_discount;
$fee_fine = $fee_fine + $fee_deposits_value->amount_fine;
$total_amount = $total_amount + $fee_value->amount;
$total_discount_amount = $total_discount_amount + $fee_discount;
$total_deposite_amount = $total_deposite_amount + $fee_paid;
$total_fine_amount = $total_fine_amount + $fee_fine;
$feetype_balance = $fee_value->amount - ($fee_paid + $fee_discount);
$total_balance_amount = $total_balance_amount + $feetype_balance;
?>
<tr>
<td><?php echo $fee_value->name ?></td>
<td><?php echo $fee_value->type ?></td>
<td><?php echo $fee_value->amount ?></td>
<td>
<div class="btn-group">
<?php
if ($feetype_balance > 0)
?>
<a href="<?php echo base_url() . 'payment/paytm/' . $fee->id . "/" . $fee_value->fee_groups_feetype_id . "/" . $_SESSION['studentDetails'][0]->id ?>" class="btn btn-xs btn-primary pull-right myCollectFeeBtn"> Pay </a>
<?php
else ?>
<h4 class="label label-success"> Paid </h4>
<?php
?>
</div>
</td>
</tr>
<?php
$finalGroupId = $group_id;
?>
</tbody>
</table>
将输出显示为,
这里每个组都有三种不同类型的费用,我只需要在一个组下显示这些类型的总和,
Fee Group Fee Amount Action
Ist Term - LKG 6000 Pay
IInd Term - LKG 4500 Pay
以上是想要的输出..
我在 foreach 循环中给出了如下内容,
if ($group_id !== $finalGroupId)
$group_total += $fee_value->amount;
echo $group_total;
如果通过迭代的费用组id与fee_value->fee_groups_id不匹配,则进行值的总和,但它不起作用..
请帮助我在 foreach 循环中获得所需的输出..
var_export($student_due_fee);
给出以下结果,
array ( 0 => stdClass::__set_state(array( 'id' => '19', 'student_session_id' => '278', 'fee_session_group_id' => '15', 'is_active' => 'no', 'created_at' => '2018-04-17 05:18:41', 'name' => 'Ist Term - LKG', 'fees' => array ( 0 => stdClass::__set_state(array( 'id' => '19', 'student_session_id' => '278', 'fee_session_group_id' => '15', 'is_active' => 'no', 'created_at' => '2018-04-17 05:18:41', 'fee_groups_feetype_id' => '74', 'amount' => '1000.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '69', 'name' => 'Ist Term - LKG', 'feetype_id' => '1', 'code' => 'ISEMTUTION', 'type' => 'I SEM TUTION FEES', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), 1 => stdClass::__set_state(array( 'id' => '19', 'student_session_id' => '278', 'fee_session_group_id' => '15', 'is_active' => 'no', 'created_at' => '2018-04-17 05:18:41', 'fee_groups_feetype_id' => '75', 'amount' => '2000.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '69', 'name' => 'Ist Term - LKG', 'feetype_id' => '2', 'code' => 'IITUTION', 'type' => 'II SEM TUTION FEES', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), 2 => stdClass::__set_state(array( 'id' => '19', 'student_session_id' => '278', 'fee_session_group_id' => '15', 'is_active' => 'no', 'created_at' => '2018-04-17 05:18:41', 'fee_groups_feetype_id' => '76', 'amount' => '3000.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '69', 'name' => 'Ist Term - LKG', 'feetype_id' => '3', 'code' => 'SPORTS', 'type' => 'SPORTS FEES', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), ), )), 1 => stdClass::__set_state(array( 'id' => '65', 'student_session_id' => '278', 'fee_session_group_id' => '16', 'is_active' => 'no', 'created_at' => '2018-04-17 05:20:20', 'name' => 'IInd Term - LKG', 'fees' => array ( 0 => stdClass::__set_state(array( 'id' => '65', 'student_session_id' => '278', 'fee_session_group_id' => '16', 'is_active' => 'no', 'created_at' => '2018-04-17 05:20:20', 'fee_groups_feetype_id' => '77', 'amount' => '2000.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '70', 'name' => 'IInd Term - LKG', 'feetype_id' => '7', 'code' => 'MAGAZINE', 'type' => 'MAGAZINE, PHOTOS,BAG, ID etc ', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), 1 => stdClass::__set_state(array( 'id' => '65', 'student_session_id' => '278', 'fee_session_group_id' => '16', 'is_active' => 'no', 'created_at' => '2018-04-17 05:20:20', 'fee_groups_feetype_id' => '78', 'amount' => '1500.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '70', 'name' => 'IInd Term - LKG', 'feetype_id' => '8', 'code' => 'SCHOLASTICS', 'type' => 'CO - SCHOLASTICS', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), 2 => stdClass::__set_state(array( 'id' => '65', 'student_session_id' => '278', 'fee_session_group_id' => '16', 'is_active' => 'no', 'created_at' => '2018-04-17 05:20:20', 'fee_groups_feetype_id' => '79', 'amount' => '1000.00', 'due_date' => '2018-04-24', 'fee_groups_id' => '70', 'name' => 'IInd Term - LKG', 'feetype_id' => '9', 'code' => 'BOOKSUNIFORM', 'type' => 'BOOKS, NOTEBOOKS, UNIFORM', 'student_fees_deposite_id' => '0', 'amount_detail' => '0', )), ), )), )
【问题讨论】:
but i couldn't get the result..
是什么意思
向我们展示var_export($student_due_fee);
的输出?给我们看一看。在问题中添加它
@AlivetoDie,输出已添加到问题中..
【参考方案1】:
你分配这么远到晚了
$finalGroupId = $group_id;
你在循环中检查它,但它在那里没有定义。
我不知道你打算做什么,否则我会帮助你。此外,通过严格类型检查!==
进行检查的方式,组 ID 永远不会等于最终组,因此无论如何它都会添加它们。
我个人会做一个数组。
$totals = [];
而不是检查这个
if ($group_id !== $finalGroupId)
$group_total += $fee_value->amount;
echo $group_total;
我只需在数组中添加一个键(数组键是唯一的)
$totals[$group_id] = $fee_value->amount;
然后在循环之后就做
$total = array_sum($totals);
更新
你说:
@ArtisticPhoenix,如果组 id 不同,我们可以将特定组 id 的总量相加,这样我就制作了该代码。但我知道这是错误的方法,这就是我寻求帮助的原因。
我回答说:
如果它不同,你怎么能把那个特定的组 id 相加,这真的没有意义。您可以对一组相同的组 id 进行求和,但是当它不同时,您不能对特定的组 id 进行求和,因为它与什么不同。
如果你想对组 id 求和,你可以检查你是否已经在数组中设置了它,然后你可以添加到它。
if(!isset($totals[$group_id]))
//first time add it to array
$totals[$group_id] = $fee_value->amount;
else
//other times add to existing value (AKA SUM)
$totals[$group_id] += $fee_value->amount;
正如我所说,你不能总结一个不同的,因为它不同于什么。
如果你的意思是它与其他一切都不同,那么你无法总结它,因为它以前从未见过。这意味着你有一个值,你不能对一个值求和。
如果您的意思是它与当前的不同,那么每次您发现与当前不同的一个时,您都会恢复其他所有内容,这在大多数情况下都是浪费精力。
因此,将一个与前一个相同的相加比较容易,如果这就是你的意思,那么这次更新就是你想要的。
如果您的意思是仅对唯一组 ID 求和,我已经在此更新之前向您展示了这一点。因为数组键是唯一的,所以 $totals
数组中的每个项目只能包含一个值,它是最后一个值 presumably
与具有相同 id 的另一个值相同。因此,totals 数组包含每个唯一 id 的值,然后我们对其求和(或不求和,取决于您真正想做的令人困惑的事情)。
【讨论】:
Op 做了很多事情。我不认为这是必要的。我认为 array_chunk(of 3) 会为他完成这项工作。 @ArtisticPhoenix,如果组 id 不同,我们可以将特定组 id 的总量相加,这样我就制作了该代码。但我知道这是错误的方法,这就是我寻求帮助的原因。 如果它不同,你怎么能总结that particular group id
,那真的没有意义。您可以对一组相同的组 id 进行求和,但是当它不同时,您不能对特定的组 id 进行求和,因为它与什么不同。以上是关于如何在php中对组中的值求和?的主要内容,如果未能解决你的问题,请参考以下文章