Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to overwrite the elements of one array with values from another – without creating additional elements in the process.

For example:

$base = array('a' => 'apple', 'b' => 'banana');
$replace = array('a' => 'orange', 'b' => 'grape', 'c' => 'cauliflower');

Merge the arrays to create:

array('a' => 'orange', 'b' => 'grape'); // 'c' not included

Using array_merge or array_replace would properly overwrite the elements, but the resulting array would include elements not found in the first array.

How can I combine two arrays to create an array containing only keys from the first array, and the corresponding values from a second array?

Is there an existing PHP array function that can do this?

Thanks for your help!

share|improve this question

4 Answers 4

up vote 4 down vote accepted

You can use array_intersect_key and array_merge to do it:

$result = array_merge($base, array_intersect_key($replace, $base));

array_intersect_key isolates those elements of $replace with keys that already exist in $base (ensuring that new elements will not appear in the result) and array_merge replaces the values in $base with these new values from $replace (while ensuring that keys appearing only in $base will retain their original values).

See it in action.

It is interesting to note that the same result can also be reached with the order of the calls reversed:

$result = array_intersect_key(array_merge($base, $replace), $base);

However this version does slightly more work, so I recommend the first one.

share|improve this answer
    
why not just array_intersect_key($replace, $base) ? while checking the documentation [link] php.net/manual/en/function.array-intersect-key.php "Also notice that the values for the keys 'blue' and 'green' differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of array1." So i dont think array_merge is required.. –  coderkane Aug 14 '13 at 19:41
    
@coderkane: Because if $replace does not contain all the keys that $base has the result will have missing values. –  Jon Aug 14 '13 at 20:24
    
I am aware, but the OP did say he wants values only from the second array. However, it is possible that he didn't foresee that scenario, so good point out. –  coderkane Aug 14 '13 at 20:30
    
@coderkane: "Overwrite elements of one array with values from another" -- IMHO this says the elements not in the other array should retain their existing values. –  Jon Aug 14 '13 at 20:37
print_r(array_intersect_key($replace, $base));
share|improve this answer
    
This looks correct and about as efficient as they come. –  Floris Aug 14 '13 at 19:47
    
Thanks everyone! I knew there had to be a simple solution. –  Jason Aug 14 '13 at 20:14
1  
@Jason: Please be aware that if $replace does not have values for all the keys in $base then the "missing" keys will not appear in the result at all. –  Jon Aug 14 '13 at 20:25

I can't think of a built in method for that, however, it would be trivial with a loop and array_key_exists.

foreach( $replace as $k => $v )
{
   if ( array_key_exists( $k, $base ) )
      $base[ $k ] = $v;
}
share|improve this answer

the following should do it:

foreach ($replace as $k => $v)
   if (isset($base[$k])) $base[$k]=$v;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.