18

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!

5 Answers 5

23

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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: Because if $replace does not contain all the keys that $base has the result will have missing values.
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: "Overwrite elements of one array with values from another" -- IMHO this says the elements not in the other array should retain their existing values.
2
print_r(array_intersect_key($replace, $base));

3 Comments

Thanks everyone! I knew there had to be a simple solution.
@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.
Note that in this version, the resulting array will have keys in the order they appear in $replace, not $base - in case it matters
0

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;
}

Comments

0

the following should do it:

foreach ($replace as $k => $v)
   if (isset($base[$k])) $base[$k]=$v;

Comments

0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.