0

私は2つのデータ配列を持っています。最初の配列には販売マネージャーのデータが含まれ、2 番目の配列には各スコアの合計値が含まれます。

以下のコードは、販売マネージャーの詳細を示しており、正常に動作します。

<?php  foreach ($allsales as $sales):?>
<?php if ($z != $sales['class']): ?>

  <tr>
  <td colspan = "6"><?php echo $sales['scode'];?><hr /> </td>
  </tr>

 <?php endif; ?>
 <tr>
  <td colspan = "2"><?php echo $sales['numb']?></td>
    <td><?php echo $sales['name']?></td>
    <td><?php echo $sales['scode']?></td>
    <td style="text-align: center"><?php echo $sales['months']?> Months</td>


    <?php
    $pending_unf =  $sales['amount'] * $sales['months']; 
    $pending = number_format((float)$pending_unf, 2, '.', '');
    ?>
    <td style="text-align: right"><?php echo $pending;?></td>

  </tr>
<tr>
  <td colspan = "6">Total Amount : XXXX</td>
  </tr>

  <?php $z = $sales['scode']; ?>
  <?php endforeach;?>

以下は私の2番目の配列データです

array(11) {
  [0]=>
  array(2) {
    ["scode"]=>
    string(3) "10A"
    ["amount"]=>
    string(5) "12600"
  }
  [1]=>
  array(2) {
    ["scode"]=>
    string(3) "10B"
    ["amount"]=>
    string(5) "51600"
  }
  [2]=>
  array(2) {
    ["scode"]=>
    string(3) "11A"
    ["amount"]=>
    string(5) "60000"
  }
  [3]=>
  array(2) {
    ["scode"]=>
    string(3) "9B"
    ["amount"]=>
    string(5) "30000"
  }

私がやりたいことは、2番目の配列から金額を取得し、以下のように各スコアグループの最後に新しい行として合計として表示することです

admission  name   months  scode
==================================
001        test1  3       10A
002        test2  5       10A
                Total    USD 1000

006        test3  7       15B
                Total    USD 1800

008        test4  1       15A
                Total    USD 800

011        test5  2       16C
                Total    USD 1600

051        test6  3       16A
012        test7  3       16A
                 Total    USD 2700

foreach を使用しようとしましたが、すべての行で繰り返されます。しかし、各グループの最後にのみ表示したいのです。

では、2 番目の配列の個々の値にアクセスし、それを条件ステートメントで使用して、各グループの最後の scode の最後に表示するにはどうすればよいでしょうか?

どんな助けでも大歓迎です。

4

1 に答える 1

0

私がすることは、2 つのループと「既に読み取られた」scode の配列を使用することです。最初のループで、最初の配列で新しいコードを見つけるたびに、それを「読み取りコード」の配列に入れ、現在の位置からすべての配列を調べてユーザーを読み取る内側のループに切り替えます。同じスコアで。このように(secon配列で「合計値」を見つける機能があると仮定します。

<?php
  $array1 = /*array of users*/;
  $array2 = /*array of total amouns of whatever*/;
  $read = array();  //theese scodes should be skipped next time
  for($i=0; $i<count($array1); $i++) {
    if(in_array($array1[$i]['scode'],$read))
      continue;  //If this user was already outputed, we skip him
                 //Othewise, we output all users with same SCODE
    $read[] = $array1[$i]['scode'];
    for($j=$i; $j<count($array1); $j++) {   //Note that I start from current position, all inputs before must be already read
      echo "User: {$array1[$j]['name']}\nScode: {$array1[$j]['scode']}\n\n";
    }
    echo "Total amount for theese users: ".get_total_amount($array1[$i]['scode'],$array2).PHP_EOL.PHP_EOL.PHP_EOL;  //finding amount for current scode.
  }

?>
于 2012-11-22T17:06:36.397 に答える