Merging two arrays in PHP
        Posted  
        
            by Industrial
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Industrial
        
        
        
        Published on 2010-05-22T22:51:36Z
        Indexed on 
            2010/05/22
            23:00 UTC
        
        
        Read the original article
        Hit count: 239
        
Hi everyone,
I am trying to create a new array from two current arrays. Tried array_merge, but it will not give me what I want. $array1 is a list of keys that I pass to a function. $array2 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 $result array.
It goes a little something like this:
$array1 = array('item1', 'item2', 'item3', 'item4');
$array2 = array(
    'item1' => 'value1',
    'item2' => 'value2',
    'item3' => 'value3'
);
Here's the result I want:
$result = array(
    'item1' => 'value1',
    'item2' => 'value2',
    'item3' => 'value3',
    'item4' => ''  
);
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 'item4' is not in $array2, based on the example.
foreach ($keys as $k => $v){
    @$array[$v] = $items[$v]; 
}
So, what's the fastest (performance-wise) way to accomplish the same result?
© Stack Overflow or respective owner