PHP - replace values of array with another array

后端 未结 5 1316
别那么骄傲
别那么骄傲 2021-01-19 14:56

Is there a way i can replace the values of one array with the values of another which has identical keys?

$arr1 = Array
        (
            [key1] => va         


        
5条回答
  •  面向向阳花
    2021-01-19 15:11

    If the keys in array 1 and 2 are identical:

    $arr1 = $arr2;
    

    If all keys of array 2 are guaranteed to be in array 1 (array 2 is a subset of array 1):

    $arr1 = array_merge($arr1, $arr2);
    

    If some keys of array 2 are not in array 1 and you want only the keys that are in array 1 to be replaced (array 2 is not a subset of array 1, and you only want to merge the intersecting part):

    $arr1 = array_merge($arr1, array_intersect_key($arr2, $arr1));
    

提交回复
热议问题