title: PHP - overwrite values in array1 using array2 only if keys exist in array1
body: <p>I would like to set an associative array of default keys and values(array1);
  If an array is provided (array2) then all indexes from array1 will be overwritten by array2 where the keys match. 
  Any additional keys in array2 wont be added to array1.</p>
  
  <p>One solution is to run a loop and do a <code>array_key_exists</code> like so. </p>
  
  <pre><code>$new_array = $array1;
  
  foreach ($array2 as $key =&gt; $value) {
  
      if(array_key_exists($key, $array1)){
          $new_array[$key] = $value; //overwrite default value
      } else {
          // new key, dont add 
      }
  }
  </code></pre>
  
  <p>but is there a php function which does this already ? </p>
  
  <p>the functions i have tries already dont give my the results i would like. </p>
  
  <pre><code>    $array1 = array(
          'one' =&gt;'one',
          'two' =&gt; 'two',
          'three' =&gt; 'three',
          'four' =&gt; 'four'
      );
      $array2 = array(
          'two' =&gt; '2',
          'four' =&gt; '4',
          'six' =&gt; '6',
      );
  
      var_dump($array1+$array2);
      array (size=5)
          'one' =&gt; string 'one' (length=3)
          'two' =&gt; string 'two' (length=3)
          'three' =&gt; string 'three' (length=5)
          'four' =&gt; string 'four' (length=4)
          'six' =&gt; string '6' (length=1)
  
      var_dump(array_merge($array1,$array2));
      array (size=5)
          'one' =&gt; string 'one' (length=3)
          'two' =&gt; string '2' (length=1)
          'three' =&gt; string 'three' (length=5)
          'four' =&gt; string '4' (length=1)
          'six' =&gt; string '6' (length=1)
  
      var_dump(array_replace($array1,$array2));
      array (size=5)
          'one' =&gt; string 'one' (length=3)
          'two' =&gt; string '2' (length=1)
          'three' =&gt; string 'three' (length=5)
          'four' =&gt; string '4' (length=1)
          'six' =&gt; string '6' (length=1)
  </code></pre>
  
  <p>The result i am looking for would be </p>
  
  <pre><code>    array (size=4)
          'one' =&gt; string 'one' (length=3)
          'two' =&gt; string '2' (length=1)
          'three' =&gt; string 'three' (length=5)
          'four' =&gt; string '4' (length=1)
  </code></pre>