如何替换两个相同的键数组并使任何设置键都为空

Let say we have 2 flowing arrays

$arr1= array('a' => "XL");  
$arr2= array('a' => "XLd",'b'=>"CDW");   

i need to assign $arr1 to $arr2 and make other keys empty whith a builtin function not foreach.

It should looks as bellow:

array(2) { ['a']=> string(2) "XL" ['b']=> string(0) ""}   

thanks

This seems fun enough :)

<?php
$arr1= array('a' => "XL");  
$arr2= array('a' => "XLd",'b'=>"CDW");

array_walk(
    $arr2, 
    function (&$val ,$key) use ($arr1){
        if (isset($arr1[$key])) {
            $val = $arr1[$key];
        } else {
            $val = '';
        }
    }
);

print_r($arr2);
//Array
//(
//[a] => XL
//[b] =>  
//)

See it here: https://3v4l.org/B7G5p

If I understand your question correctly, this should do:

$out = array_merge(
    $arr2,
    array_fill_keys(array_keys($arr2), ''),
    $arr1
);

Another oneliner solution:

$arr2 = array_replace(array_map(function($n){return "";}, $arr2), $arr1);

https://3v4l.org/cgBB1