foreach

时间:2018-04-20 00:15:20

标签: php codeigniter foreach

我正在嵌套foreach,

 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
     }
 }
?>

结果表看起来像,

   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

print_r($fee_value)给出以下内容,

    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 ) 

如果与组ID和父组ID匹配,我需要两个组的总和,我希望的结果是,

   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; 

如果父组ID(69)与费用组ID(69)匹配,那么它们将组合为单个组,我需要总结两行的数量并将其显示为一个。这里三个金额需要与第一组中的金额相加,因为与父组匹配。

在foreach中,我试过了,如果喜欢,

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";
       }

    }
 }
?>

但它对我没有帮助。任何给我一个解决方案的帮助,就像我提到的表格一样,我会更加明显......

1 个答案:

答案 0 :(得分:2)

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>';