title: php insert value into array of arrays using foreach
body: <p>I have a pretty basic question but I am stuck. I am pretty new to php and I have an array like this:</p>
  
  <pre><code>$array = array(
      'one' =&gt; 1,
      'two' =&gt; array('key1' =&gt; 'val1','key2' =&gt; 'val2'),
      'three' =&gt; array('key1' =&gt; 'val1','key2' =&gt; 'val2'),
      'four' =&gt; array('key1' =&gt; 'val1','key2' =&gt; 'val2')
  );
  </code></pre>
  
  <p>and for each of the arrays in the array (that is, 'two, 'three', and 'four'), I want to insert 'key3' => 'val3' into those arrays.</p>
  
  <p>I tried this:</p>
  
  <pre><code>foreach($array as $item) {
      if (gettype($item) == "array") {
          $item['key3'] = 'val3';
      }
  }
  </code></pre>
  
  <p>But it doesn't work, and I'm not sure why. Using various print_r's all over the place, it seems to insert 'key3' => 'val3' into $item if I print it out in the loop, but the original array seems unchanged. I also tried a regular for loop but that didn't work either.</p>