title: Merging two arrays in PHP
body: <p>I am trying to create a new array from two current arrays. Tried array_merge, but it will not give me what I want. <code>$array1</code> is a list of keys that I pass to a function. <code>$array2</code> holds the results from that function, but doesn't contain any non-available resuls for keys. So, I want to make sure that all requested keys comes out with 'null':ed values, as according to the shown <code>$result</code> array.</p>
  
  <p>It goes a little something like this:</p>
  
  <pre><code>$array1 = array('item1', 'item2', 'item3', 'item4');
  
  $array2 = array(
      'item1' =&gt; 'value1',
      'item2' =&gt; 'value2',
      'item3' =&gt; 'value3'
  );
  </code></pre>
  
  <p>Here's the result I want:</p>
  
  <pre><code>$result = array(
      'item1' =&gt; 'value1',
      'item2' =&gt; 'value2',
      'item3' =&gt; 'value3',
      'item4' =&gt; ''  
  );
  </code></pre>
  
  <p>It can be done this way, but I don't think that it's a good solution - I really don't like to take the easy way out and suppress PHP errors by adding @:s in the code. This sample would obviously throw errors since <code>'item4'</code> is not in <code>$array2</code>, based on the example.</p>
  
  <pre><code>foreach ($keys as $k =&gt; $v){
      @$array[$v] = $items[$v]; 
  }
  </code></pre>
  
  <p>So, what's the fastest (performance-wise) way to accomplish the same result?</p>