0

我有一个数组,其中包含带有分类的用户数据。我尝试在最后一个分类组的末尾通过数组的末尾输出总和

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

前锋:

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

所以我会得到一个包含用户数据列表和总数的表格,然后是另一组用户。

关于如何实现这一目标的任何帮助?

4

2 回答 2

2

尝试如下

<?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;?>
于 2012-11-26T16:50:58.870 回答
0

如果您的数组索引顺序正确,则在 foreach 循环中使用计数器变量或 foreach ($array as $key=>$val) 然后检查

if ($key > 0 and ($array[$key-1]['classifi'] != $val['classifi'] 
    or $key == count($array) - 1)) { }

然后输出总行并将组总和设置为0

于 2012-11-26T16:36:45.867 回答