Q:

output new html row after the end of value in array

im having a array which contains user data with a classification. im try to output the total_sum at the end of the last classification group throught the end of the array
array(22) {
  [0]=>
  array(5) {
    ["reg_numb"]=>
    string(4) "4334"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(20) "tregfdgfd"
    ["classifi"]=>
    string(2) "1A"
    ["total_sum"]=>
    string(5) "45000"
  }
  [1]=>
  array(5) {
    ["reg_numb"]=>
    string(4) "4336"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(19) "ewrtgfgdfg"
    ["classifi"]=>
    string(2) "1A"
    ["total_sum"]=>
    string(5) "45000"
  }
  [2]=>
  array(5) {
    ["reg_numb"]=>
    string(4) "4340"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(16) "ihjkhjbmgy"
    ["classifi"]=>
    string(2) "1A"
    ["total_sum"]=>
    string(5) "45000"
  }
  [3]=>
  array(5) {
    ["reg_numb"]=>
    string(4) "4394"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(20) "ertgfbfjghj"
    ["classifi"]=>
    string(2) "1B"
    ["total_sum"]=>
    string(5) "30000"
  }
  [4]=>
  array(5) {
    ["reg_numb"]=>
    string(4) "4396"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(21) "dwerdsfdf"
    ["classifi"]=>
    string(2) "1B"
    ["total_sum"]=>
    string(5) "30000"
  }
  [5]=>
  array(5) {
    ["reg_numb"]=>
    string(4) "3496"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(23) "rergfdg"
    ["classifi"]=>
    string(2) "3A"
    ["total_sum"]=>
    string(5) "35000"
  }
  [6]=>
  array(5) {
    ["reg_numb"]=>
    string(4) "3796"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(19) "ghjyuytuyt"
    ["classifi"]=>
    string(2) "3A"
    ["total_sum"]=>
    string(5) "35000"
  }
  [7]=>
  array(5) {
    ["reg_numb"]=>
    string(4) "3797"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(25) "qeevvf"
    ["classifi"]=>
    string(2) "3B"
    ["total_sum"]=>
    string(5) "35000"
  }
  [8]=>
  array(5) {
    ["reg_numb"]=>
    string(4) "3827"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(17) "qvfrrere"
    ["classifi"]=>
    string(2) "3B"
    ["total_sum"]=>
    string(5) "35000"
  }
  [9]=>
  array(5) {
    ["reg_numb"]=>
    string(4) "1921"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(28) "ewrewrsdf"
    ["classifi"]=>
    string(2) "6A"
    ["total_sum"]=>
    string(5) "37000"
  }
  [10]=>
  array(5) {
    ["reg_numb"]=>
    string(4) "2324"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(23) "abcdddsf"
    ["classifi"]=>
    string(2) "6A"
    ["total_sum"]=>
    string(5) "37000"
  }
  [11]=>
  array(5) {
    ["months"]=>
    string(2) "10"
    ["reg_numb"]=>
    string(4) "1922"
    ["month"]=>
    string(10) "2012-01-01"
    ["name"]=>
    string(22) "test123"
    ["classifi"]=>
    string(2) "6B"
    ["total_sum"]=>
    string(5) "37000"
  }

foreach:
  <?php  foreach ($infos as $info):?>
 <tr>
      <td colspan = "2"><?php echo $info['reg_numb']?></td>
        <td><?php echo $info['name']?></td>
        <td><?php echo $info['classifi']?></td>
        <td style="text-align: center"><?php echo $info['months']?>Months</td>

    if its the last row in a classfi grpup out put the below row with total for that classifi
    <tr>
      <td colspan = "3">Total</td>
      <td style="text-align: right"><hr /><?php echo $info[total_sum]?> </td>
    </tr>

    begin the next classifi group
    <?php endforeach;?>

so i will get a table with the list of user data and the total and then the other group of users. 
any help on how to achieve this?

A:

Try as below
<?php
   $new_data = array();
   foreach ($infos as $key => $info):
      $new_data[$info['classifi']][] = $info;
   endforeach;
?>

<?php foreach ($new_data as $key1 => $arrinfo):?>
<?php  foreach ($arrinfo as $key => $info):?>
 <tr>
      <td colspan = "2"><?php echo $info['reg_numb']?></td>
        <td><?php echo $info['name']?></td>
        <td><?php echo $info['classifi']?></td>
        <td style="text-align: center"><?php echo $info['months']?>Months</td>

//    if its the last row in a classfi grpup out put the below row with total for that classifi

   // Check everytimg is it last record
   <?php if($key==count($arrinfo)-1) {?>
    <tr>
      <td colspan = "3">Total</td>
      <td style="text-align: right"><hr /><?php echo $info[total_sum]?> </td>
    </tr>
   <?php } ?>   
   // begin the next classifi group
<?php  endforeach;?>
<?php endforeach;?>

