title: Add values to an array inside a foreach loop
body: <p>I'm trying to edit an array on the fly, inside a <code>foreach</code> loop. I basically analyse each key, and if this key match the one I want, I want to add another entry in the array <strong>immediately after this one</strong>.</p>
  
  <p>If I take this code,</p>
  
  <pre><code>$values = array(
      'foo' =&gt; 10,
      'bar' =&gt; 20,
      'baz' =&gt; 30
  );
  
  foreach($values as $key =&gt; $value){
      print $value . ' ';
      if($key == 'bar'){
          $values['qux'] = 21;
      }
  }
  </code></pre>
  
  <p>I've got 2 problems,</p>
  
  <ul>
  <li>first, the output is <code>10 20 30</code> instead of the expected <code>10 20 30 21</code></li>
  <li>second, even if I solve the first problem, my value will still be added <em>at the end</em> of my array</li>
  </ul>
  
  <p><strong>How could I add the <code>qux</code> entry between <code>bar</code> and <code>baz</code> ones?</strong></p>
  
  <p>Thanks for your ideas.</p>