title: Create new array within existing array with each loop in foreach
body: <p>I have a foreach loop which, for each pension ID, i am using the pension amount that corresponds to that ID and applying an inflation rate to the pension value over 30 years. There will always be a variable amount of unique pensions for each person.</p>
  
  <p>In the foreach loop I am able to run it and have it produce the future value of the pension amount over time, iterating each years increased values. The problem i am running into is that for each pension id it is adding the new pension values into the name array. </p>
  
  <p>For example, if the individual has 4 pensions, that produces 120 array values in one array. What I am wanting is 4 (or how many pensions are there) arrays with each unique pension values in its own array. My goal is to then add each pension year from each array  together and come to a total pension amount for the individual for each year. </p>
  
  <p>Example.<br>
  <code>pension 1 array([1] =&gt; 100 [2] =&gt; 200 [3] =&gt; 300…….</code><br>
  <code>pension 2 array([1] =&gt; 200 [2]=&gt; 300 [3] =&gt; 400……</code></p>
  
  <p>(then adding each array together based on key)<br>
  <code>Total pension amount array([1] =&gt; 300 [2] =&gt; 500 [3] =&gt; 700</code></p>
  
  <p>I have been trying all sorts of things but can’t find a solution, Im thinking the foreach might not be able to accomplish this.</p>
  
  <p>To to recap the solution I need, after each pass of the foreach I need to create a new array with the new values and not have them add to the existing array.</p>
  
  <p>Any help would be wonderful.</p>
  
  <p>Here is my existing code.</p>
  
  <p>note: everything is going into $pension[]</p>
  
  <pre><code>foreach($pen_start as $key =&gt; $value){
  
      if($value &lt; $age){
          if($pen_cola[$key] == 'Yes'){
              $i = 0;
              $a = $age;
              $previous = $pen_amount[$key];
              while($i &lt;= 29 ){
              $pension[] = str_replace(',','',number_format(($previous*1.02),2));
              $previous = $previous*1.02;
              $i++;
              $a++;
              };
              // if yes end
          } else if($pen_cola[$key] == 'No' || $pen_cola[$key] == ''){
  
              $i = 0;
              $a = $age;
              $previous = $pen_amount[$key];
              while($i &lt;= 29 ){
  
              $pension[] = str_replace(',','',number_format(($previous),2));
              $previous = $previous;
              $i++;
              $a++;
              };
          } // if no end
      } // if older end
      else if($value &gt; $age){
          if($pen_cola[$key] == 'Yes'){
              $i = 0;
              $a = $age;
              $previous = 0;
              while($i &lt;= 29 ){
  
                  if($a &lt; $value){
                      $amount = 0;
                  }else if($previous == 0){
                      $amount = $pen_amount[$key];}
                      else {$amount = $previous;}
  
              $pension[] = str_replace(',','',number_format(($amount*1.02),2));
              $previous = $amount*1.02;
              $i++;
              $a++;
              };
              // end if yes
          }  else if($pen_cola[$key] == 'Yes'){
              $i = 0;
              $a = $age;
              $previous = 0;
              while($i &lt;= 29 ){
  
                  if($a &lt; $value){
                      $amount = 0;
                  }else if($previous == 0){
                      $amount = $pen_amount[$key];}
                      else {$amount = $previous;}
  
              $pension[] = str_replace(',','',number_format(($amount),2));
              $previous = $amount;
              $i++;
              $a++;
              };
          } //end if no
      } // if younger end
  } // end foreach
  </code></pre>