Maniraj Murugan
Maniraj Murugan

Reputation: 9084

Sum of two group values inside foreach

I am having nested foreach like,

 foreach ($student_due_fee as $key => $fee) {

     foreach ($fee->fees as $fee_key => $fee_value) {

     ?>

        <tr>

          <td> <?php echo $fee_value->name; ?></td>
          <td> <?php echo $fee_value->fee_groups_id; ?></td>
          <td> <?php echo $fee_value->parent_fee_groups_id; ?></td>
          <td> <?php echo $fee_value->total_amount ?> </td>
          <td> <?php echo $fee_value->group_fee_type ?> </td>
          <td> <?php echo $fee_value->group_fee_amount ?> </td>

      </tr>
   <?php
     }
 }
?>

And the result table looks like,

   group_name    group_id    parent_group_id     amount   group_type

     One           69             0               3300   I SEM TUTION FEES; II SEM TUTION FEES;
     Two           70             0               450    CO - SCHOLASTICS; BOOKS; 
    Three          71            69              -100    CONCESSION

And the print_r($fee_value) gives the following,

    stdClass Object ( [id] => 2 [student_session_id] => 278 [fee_session_group_id] => 15 [is_active] => no [created_at] => 2018-04-19 11:36:57 [fee_groups_feetype_id] => 74 [group_fee_type] => I SEM TUTION FEES; II SEM TUTION FEES; SPORTS FEES [group_fee_amount] => 1000.00; 2000.00; 300.00 [amount] => 1000.00 [total_amount] => 3300.00 [due_date] => 2018-04-24 [fee_groups_id] => 69 [parent_fee_groups_id] => 0 [name] => Ist Term - LKG [feetype_id] => 1 [code] => ISEMTUTION [type] => I SEM TUTION FEES [student_fees_deposite_id] => 0 [amount_detail] => 0 ) 

    stdClass Object ( [id] => 48 [student_session_id] => 278 [fee_session_group_id] => 16 [is_active] => no [created_at] => 2018-04-19 11:37:36 [fee_groups_feetype_id] => 77 [group_fee_type] => MAGAZINE, PHOTOS,BAG, ID etc ; CO - SCHOLASTICS; BOOKS, NOTEBOOKS, UNIFORM [group_fee_amount] => 200.00; 150.00; 100.00 [amount] => 200.00 [total_amount] => 450.00 [due_date] => 2018-04-24 [fee_groups_id] => 70 [parent_fee_groups_id] => 0 [name] => IInd Term - LKG [feetype_id] => 7 [code] => MAGAZINE [type] => MAGAZINE, PHOTOS,BAG, ID etc [student_fees_deposite_id] => 0 [amount_detail] => 0 ) 

    stdClass Object ( [id] => 93 [student_session_id] => 278 [fee_session_group_id] => 20 [is_active] => no [created_at] => 2018-04-19 11:40:36 [fee_groups_feetype_id] => 84 [group_fee_type] => CONCESSION [group_fee_amount] => -100.00 [amount] => -100.00 [total_amount] => -100.00 [due_date] => 1970-01-01 [fee_groups_id] => 71 [parent_fee_groups_id] => 69 [name] => DISCOUNT [feetype_id] => 15 [code] => CONCESSION [type] => CONCESSION [student_fees_deposite_id] => 0 [amount_detail] => 0 ) 

Here i am in the need of sum of two groups if there is a matching with group id and parent group id, and my desired result is like,

   group_name    group_id    parent_group_id     amount   group_type

     One           69             0               3200   I SEM TUTION FEES; II SEM TUTION FEES; CONCESSION
     Two           70             0               450    CO - SCHOLASTICS; BOOKS; 

If parent group id (69) matches with fee group id (69), then they will combine as single group and i need to sum up the amount of both row and display it as one. Here the amount in three needs to get sum up with the amount in group one as there is a match with parent group.

Inside foreach i have tried with if like,

foreach ($student_due_fee as $key => $fee) {

     foreach ($fee->fees as $fee_key => $fee_value) {

    if ($fee_value->parent_fee_groups_id == $fee_value->fee_groups_id) {
         echo "string";
       }

    }
 }
?>

But it doesn't help me. Any help that gives me a solution as like the table i have mentioned would me more appreciable..

Upvotes: 0

Views: 62

Answers (1)

Joseph_J
Joseph_J

Reputation: 3669

Updated:

foreach ($student_due_fee as $key => $fee) {

     foreach ($fee->fees as $fee_key => $fee_value) {

        $results[] = array(

              'group_name'      => $fee_value->name,
              'group_id'        => $fee_value->fee_groups_id,
              'parent_group_id' => $fee_value->parent_fee_groups_id,
              'amount'          => $fee_value->total_amount,
              'group_type'      => $fee_value->group_fee_type,
              'group_amount'    => $fee_value->group_fee_amount

          );

     }
 }


 for($i = 0; $i < count($results); $i++){

   for($j = 0; $j < count($results); $j++){

     if($results[$i]['group_id'] == $results[$j]['parent_group_id']){

           $results[$i]['amount'] += $results[$j]['amount'];
           $results[$i]['group_type'] .=  ' ' . $results[$j]['group_type'];
           $rowsToBeDeleted[] = $j;

     }

   }

 }


 foreach($rowsToBeDeleted as $key=>$value){

 unset($results[$value]);

 }


echo 
'<table>

  <tr>

    <th>Group Name</th>
    <th>Group ID</th>
    <th>Parent Group ID</th>
    <th>Amount</th>
    <th>Group Type</th>
    <th>Group Amount</th>



  </tr>';

foreach($results as $key){

  echo 
    '<tr>

      <td>' . $key['group_name'] . '</td>' .
      '<td>' . $key['group_id'] . '</td>' .
      '<td>' . $key['parent_group_id'] . '</td>' .
      '<td>' . $key['amount'] . '</td>' .
      '<td>' . $key['group_type'] . '</td>' .
      '<td>' . $key['group_amount'] . '</td>' .

  '</tr>';

}

echo 
'</table>';

Upvotes: 2

Related Questions