title: Unable to modify array element value
body: <p>I have some code that loops through an array, and, if an element of it has a qty of more than 1, copies that element out into as many elements as the qty and then sets the qty of all of them to 1 (and makes a few other changes.)</p>
  
  <p>I've been staring at this for a long time, and I'm sure I must be missing something obvious, because I cannot for the life of me get it to change the values I want to change.</p>
  
  <p>Here's the basic code:</p>
  
  <pre><code>foreach ($items as $id =&gt; $itm) {
      // if there's an item that has a qty &gt; 1, need to split out into multiple nodes with qty = 1
      if ($itm['qty'] &gt; 1) {
          $qtyCount = $itm['qty'];
          $newItems = array_fill($id, $qtyCount, $itm);
  
          foreach ($newItems as $newId =&gt; $n) {
              $n['qty'] = 1;
              $n['deal_id'] = $newId;
          }
  
          error_log("new items");
          error_log(print_r($newItems,1));
      }
  }
  </code></pre>
  
  <p>What I would start with is an array that looks like this:</p>
  
  <pre><code>[items] =&gt; Array
      (
          [0] =&gt; Array
              (
                  [product] =&gt; AW8B
                  [qty] =&gt; 1
                  [subtotal] =&gt; 6.500
                  [deal_id] =&gt; 
              )
  
          [1] =&gt; Array
              (
                  [product] =&gt; C
                  [qty] =&gt; 3
                  [subtotal] =&gt; 30.000
                  [deal_id] =&gt; 1
              )
  
          [2] =&gt; Array
              (
                  [product] =&gt; QUEPAP
                  [qty] =&gt; 1
                  [subtotal] =&gt; 4.000
                  [deal_id] =&gt; 
              )
  
      )
  </code></pre>
  
  <p>In this case, only element 1 in the original items array meets the criteria of qty > 1, so the code should create a new array containing three identical elements to start with. Then the <code>foreach($newItems as $newId =&gt; $n)</code> loop should loop through the new, 3-element array, and set the qty element for each one to 1 and the deal_id element to the loop index. What I would expect the <code>error_log(print_r($newItems,1))</code> line to return is something that looks like the following. </p>
  
  <pre><code>[newItems] =&gt; Array
  (
      [1] =&gt; Array 
          (
              [product] =&gt; C
              [qty] =&gt; 1
              [subtotal] =&gt; 30.000
              [deal_id] =&gt; 1
          )
  
      [2] =&gt; Array
          (
              [product] =&gt; C
              [qty] =&gt; 1
              [subtotal] =&gt; 30.000
              [deal_id] =&gt; 2
          )
  
      [3] =&gt; Array
          (
              [product] =&gt; C
              [qty] =&gt; 1
              [subtotal] =&gt; 30.000
              [deal_id] =&gt; 3
          )
  ) 
  </code></pre>
  
  <p>However, the qty remains 3 and the deal_id remains 1 for all of them, even after the internal <code>foreach</code> loop has run. This is driving me up a wall, because I'm not a newbie and have done this kind of thing before. Just cannot for the life of me figure out what I'm doing wrong here.</p>