如何在php中对组中的值求和?

btxsgosb  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(362)

我列出了一个学生的学费明细,代码是,

<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与“费用值”->“费用组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', )), ), )), )
iih3973s

iih3973s1#

你把这么远的时间分配到了很晚

$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不同。
如果您想和组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 因此,totals数组包含每个唯一id的值,然后我们对其求和(或不求和,取决于您真正想做的事情)。

相关问题