title: Adding Values together in a loop with a dynamic Array
body: <p>My current output in my foreach loop is this:<br>
  <hr>ID:21 Amount: 0.000<br>ID:1 Amount: 0.000<br>ID:2 Amount: 0.000<br>ID:3 Amount: 0.000<br>ID:5 Amount: 0.000<br>ID:8 Amount: 0.000<br><hr>ID:1 Amount: 8.000<br>ID:5 Amount: 1.000<br>ID:17 Amount: 1.000<br><hr>ID:1 Amount: 8.000<br>ID:5 Amount: 1.000<br>ID:17 Amount: 1.000<br><hr>ID:1 Amount: 8.000<br>ID:5 Amount: 1.000<br>ID:17 Amount: 1.000<br><hr>ID:1 Amount: 20.000<br>ID:5 Amount: 2.500<br>ID:17 Amount: 2.500<br>
  <hr>ID:21 Amount: 2.000<br>ID:1 Amount: 2.000<br>ID:2 Amount: 2.000<br>ID:3 Amount: 2.000<br>ID:4 Amount: 1.000<br>ID:19 Amount: 1.000<br><hr></p>
  
  <p>I want to Add together the "Amount" for each "ID" and output the totals for each "ID" at the end. The number of "ID"'s is dynamic. This is my current code:<br></p>
  
  <pre><code>$amount_for_that_group="0";
   foreach ($beneficiaryIDArray as $beneficiaryID) {
   //see what distribution group they belong to
    $query_ben_group= "SELECT * FROM `beneficiaries` WHERE `beneficiary_id`='$beneficiaryID'";
  
   $rs_ben_group=$conn-&gt;query($query_ben_group);
  
    if($rs_ben_group === false){                                                         trigger_error('SQL error:'.$query_ben_group.' Error: '.$conn-&gt;error, E_USER_ERROR);}
           else{
  
       while ($row_ben_group=$rs_ben_group-&gt;fetch_assoc()) {
            $ggroup=$row_ben_group['reward_group'];
            if ($ggroup==1) {
            $amount_for_that_group=$single_group1_amount;
            }
            if ($ggroup==2) {
            $amount_for_that_group=$single_group2_amount;
            }
            if ($ggroup==3) {
            $amount_for_that_group=$single_group3_amount;
            }
  
          }
  
        }           
  
  echo "ID:$beneficiaryID Amount: $amount_for_that_group&lt;br&gt;";
  
   }
  </code></pre>